首页 \ 问答 \ JList不会出现在JFrame上(JList won't show up on JFrame)

JList不会出现在JFrame上(JList won't show up on JFrame)

除了列表没有显示在JFrame上的事实之外,一切正常。 我不明白为什么我不能在没有声明新布局的情况下添加内容。

    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;

    import java.awt.*;
    import java.awt.event.*;

public class Gui3 extends JFrame {
    private JPanel mousepanel;
    private JLabel statusbar;
    private JList list;
    private static String[] colornames = {"black","blue","red","white"};
    private static Color[] colors = {Color.BLACK, Color.BLUE,Color.RED,Color.WHITE};

    public Gui3(){
        super("The title");

        list = new JList(colornames);
        list.setVisibleRowCount(4);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setFixedCellHeight(15);
        list.setFixedCellWidth(100);
        add(new JScrollPane(list));
        list.addListSelectionListener(
                new ListSelectionListener(){
                    public void valueChanged(ListSelectionEvent event){

用户应该能够从列表中选择背景颜色,然后程序应该将背景更改为该颜色。 但是不是......

mousepanel.setBackground(colors[list.getSelectedIndex()]);

                    }
                }
            );

        mousepanel = new JPanel();
        mousepanel.setBackground(Color.WHITE);
        add(mousepanel, BorderLayout.CENTER);

        statusbar = new JLabel("Default");
        add(statusbar, BorderLayout.SOUTH); 


        HandlerClass handler = new HandlerClass();
        mousepanel.addMouseListener(handler);
        mousepanel.addMouseMotionListener(handler);

    }   
    private class HandlerClass implements MouseListener, MouseMotionListener
        {
        @Override
        public void mouseClicked(MouseEvent event) {
            statusbar.setText(String.format("Clicked at %d, %d", event.getX(),event.getY()));
            DrawShapes shapes = new DrawShapes();
            shapes.setPosition(event.getX(), event.getY());
            add(shapes);

        }
        @Override
        public void mousePressed(MouseEvent event){
            statusbar.setText("You pressed down the mouse");
        }

        @Override
        public void mouseReleased(MouseEvent event){
            statusbar.setText("You released the button");
        }
        @Override
        public void mouseEntered(MouseEvent event){
            statusbar.setText("You entered the area");
            mousepanel.setBackground(Color.RED);
        }
        @Override
        public void mouseExited(MouseEvent event){
            statusbar.setText("The mouse has left the window");
            mousepanel.setBackground(Color.WHITE);
        }
        //These are mouse motion events
        @Override
        public void mouseDragged(MouseEvent event){
            statusbar.setText("You are dragging the mouse");
        }
        @Override
        public void mouseMoved(MouseEvent event){
            statusbar.setText("You are moving the mouse");
        }

    }
}

Everything works except for the fact that the list doesn't show up on the JFrame. I don't understand why I can't add things without declaring a new layout.

    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;

    import java.awt.*;
    import java.awt.event.*;

public class Gui3 extends JFrame {
    private JPanel mousepanel;
    private JLabel statusbar;
    private JList list;
    private static String[] colornames = {"black","blue","red","white"};
    private static Color[] colors = {Color.BLACK, Color.BLUE,Color.RED,Color.WHITE};

    public Gui3(){
        super("The title");

        list = new JList(colornames);
        list.setVisibleRowCount(4);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setFixedCellHeight(15);
        list.setFixedCellWidth(100);
        add(new JScrollPane(list));
        list.addListSelectionListener(
                new ListSelectionListener(){
                    public void valueChanged(ListSelectionEvent event){

The user should be able to select the background color from the list and then the program should change the background to that color. But is isn't...

mousepanel.setBackground(colors[list.getSelectedIndex()]);

                    }
                }
            );

        mousepanel = new JPanel();
        mousepanel.setBackground(Color.WHITE);
        add(mousepanel, BorderLayout.CENTER);

        statusbar = new JLabel("Default");
        add(statusbar, BorderLayout.SOUTH); 


        HandlerClass handler = new HandlerClass();
        mousepanel.addMouseListener(handler);
        mousepanel.addMouseMotionListener(handler);

    }   
    private class HandlerClass implements MouseListener, MouseMotionListener
        {
        @Override
        public void mouseClicked(MouseEvent event) {
            statusbar.setText(String.format("Clicked at %d, %d", event.getX(),event.getY()));
            DrawShapes shapes = new DrawShapes();
            shapes.setPosition(event.getX(), event.getY());
            add(shapes);

        }
        @Override
        public void mousePressed(MouseEvent event){
            statusbar.setText("You pressed down the mouse");
        }

        @Override
        public void mouseReleased(MouseEvent event){
            statusbar.setText("You released the button");
        }
        @Override
        public void mouseEntered(MouseEvent event){
            statusbar.setText("You entered the area");
            mousepanel.setBackground(Color.RED);
        }
        @Override
        public void mouseExited(MouseEvent event){
            statusbar.setText("The mouse has left the window");
            mousepanel.setBackground(Color.WHITE);
        }
        //These are mouse motion events
        @Override
        public void mouseDragged(MouseEvent event){
            statusbar.setText("You are dragging the mouse");
        }
        @Override
        public void mouseMoved(MouseEvent event){
            statusbar.setText("You are moving the mouse");
        }

    }
}

原文:https://stackoverflow.com/questions/36001768
更新时间:2022-07-24 14:07

最满意答案

解决方法

在一些常见的.js文件中定义

function showMyIndicator() {
    busyIndicator.__options.text="Custom loading text";
    busyIndicator.show();
}

每次调用busyIndi​​cator.show()时调用showMyIndi​​cator()。

busyIndi​​cator.hide()可以正常调用(尽管你可能想要定义一些'hideMyIndi​​cator()'函数来实现同质性)。

发生什么事?

看来,一旦我加载(jQuery $ .load())一些新内容以便更改页面,busyIndi​​cator对象的文本fileld默认为“[(')هههههههههLoading]”,这是每个messages.json中的本机项目。 那个文件充满了那些奇怪的迹象,不知道为什么。

更新

我的项目中存在一些损坏,导致生成错误字符的.json文件,新生成的项目没有显示相同的问题,因此创建一个新的并移动资源可能会有效。


A workaround

define in some common .js file

function showMyIndicator() {
    busyIndicator.__options.text="Custom loading text";
    busyIndicator.show();
}

and call showMyIndicator() each time you would call busyIndicator.show().

busyIndicator.hide() can be called normally (although you might want to define some 'hideMyIndicator()' function for homogeneity).

What is happening?

It appears that, as soon as I load (jQuery $.load() ) some new content in order to change page, the text fileld of the busyIndicator object defaults to "[(')ههههههه Loading]", which is defined in every native project inside messages.json. That file is full of those weird signs, no idea about why.

Update

There's something corrupted in my project which causes those .json files to be generated with wrong characters, a freshly generated project didn't show the same issues so creating a new one and move the resources in there might do the trick.

相关问答

更多

相关文章

更多

最新问答

更多
  • 以编程方式创建视频?(Create videos programmatically?)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • javascript数组,如何改变这个数组结构(javascript arrays, how to change this array structure)
  • 在ASP.NET Web API中使用多个Get方法进行路由(Routing with multiple Get methods in ASP.NET Web API)
  • 用于backbone.js验证的自定义验证器(Custom validator for backbone.js validation)
  • const char *与其他指针有什么不同?(Is const char * different from other pointers? [duplicate])
  • 无效的列索引,使用PreparedStatement更新(Invalid column index , update using PreparedStatement)
  • watchOS WCSession'已配对'和'watchAppAvailable'不可用(watchOS WCSession 'paired' and 'watchAppAvailable' are unavailable)
  • CalledFromWrongThreadException在Android上执行JUnit测试(CalledFromWrongThreadException exercising JUnit tests on Android)
  • 如何把文件保存到你的应用程序目录中?(How to put\save files into your application directory? (adobe air))
  • 美元符号在Java方法描述符中的含义?(Meanings of dollar sign in Java method descriptor?)
  • font-size的含义是什么:1em / 2em?(What doe the meaning of font-size:1em/2em?)
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • Android - 检测与特定wifi ssid断开连接的正确方法?(Android - Correct way to detect disconnecting from a particular wifi ssid?)
  • 通过Shell脚本将文件转换为另一个文件(Convert File To Another File By Shell Script)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • 如何过滤magento废弃的购物车报告集合(How to Filter the magento abandoned cart report collection)
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • web api http post传递对象504接收失败(web api http post passing object 504 Receive Failure)
  • Rails从视图编辑模型上的多个属性的方法(Rails way to edit multiple attributes on a model from a view)
  • 总是用{}初始化对象是否是好习惯?(Is it good habit to always initialize objects with {}?)
  • 在方案中编写特殊字符到输出端口(编译器设计)(writing special characters to output port in scheme (compiler design))
  • 电脑等级考试得证有多大用处?
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • 第一次调用函数将无法按预期工作,但下一次工作正常(calling a function on the first time won't work as expected, but next time is working)
  • 如何优化使用BigInteger操作执行时间的代码(How to optimize the code that uses BigInteger operations for execution time)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何提供个人资料信息,以便Passport.js可以使用它?(how does Profile information should be provided so Passport.js can use it?)
  • 有没有办法初始化jquery数据表中的细节?(is there any way to initialize details in jquery datatable?)