【问题标题】:Java Swing JComboBox/ Button edit parametersJava Swing JComboBox/按钮编辑参数
【发布时间】:2018-02-25 20:24:03
【问题描述】:

我对在 Java 中使用 GUI 很陌生。我尝试了几件事,例如,向 JComboBox 添加新条目或使用 run 方法中的以下命令更改 JButtons 标题:

pwSelection.addItem("名称 1");

dec_btn.setText("示例");

受保护的 JComboBox pwSelection = new JComboBox(contents);

不幸的是,一旦我启动程序,它就没有任何效果。 布局是使用 IntelliJ GUI Form Creator 制作的。 如果您能给我任何提示或替代方法,那就太好了。

package PackMain;

import javax.swing.*;

public class Password {

    String[] contents = {"Name 1", "Name 2"};
    protected JTextField newAdress;
    protected JPanel panel1;
    protected JComboBox pwSelection = new JComboBox(contents); // ?
    protected JLabel title;
    protected JTextField pwOutput;
    protected JButton enc_btn;
    protected JButton dec_btn;

    public JFrame run(){
        JFrame mainFrame= new JFrame ("Password");
        mainFrame.setContentPane(new Password().panel1);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setSize(400, 200);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);

        pwSelection.addItem("Name 1");
        dec_btn.setText("Example");

        return mainFrame;
    }


    public static void main(String[] args){
        new Password().run();
    }

}

【问题讨论】:

    标签: java button combobox items


    【解决方案1】:

    您没有向框架添加任何面板,也没有初始化任何属性...

    package password;
    
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class Password extends JFrame{
    
    String[] contents = {"Name 1", "Name 2"};
    protected JTextField newAdress;
    protected JPanel panel1, panel2; //2 panels for a more organized approach...
    protected JComboBox  pwSelection;// ?
    protected JTextField pwOutput;
    protected JButton enc_btn;
    protected JButton dec_btn;
    
    public Password(){          //Constructor of the class, initialize stuff here...
    
        super("Shinny title");
        //1. Initialize all your atributtes...
        newAdress = new JTextField();
        panel1 = new JPanel();
        panel2 = new JPanel();
        pwSelection = new JComboBox(contents);
        pwOutput= new JTextField();
        enc_btn = new JButton("First button");
        dec_btn = new JButton("Second Button");
        //Set layouts..
        panel1.setLayout(new GridLayout(3,1));          
    panel2.setLayout(new FlowLayout());
    
        //Add items to panel 1...
        panel1.add(new JLabel("Adress:"));  //A more simple aproach for creating a label...
        panel1.add(newAdress);
        panel1.add(pwSelection);
        panel1.add(enc_btn);
        panel1.add(dec_btn);
        panel1.add(pwOutput);
        //Add items of panel 1 to panel 2...
        panel2.add(panel1);
        add(panel2);
    
        setSize(300,300);
        setVisible(true);
    
        //Proper way of stop the application at closing...
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
    }
    
    //This is not necessary as everything should be implemented in the constructor.
    /*
    public JFrame run(){
        JFrame mainFrame= new JFrame ("Password");
        mainFrame.setContentPane(new Password().panel1);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setSize(400, 200);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);
    
        pwSelection.addItem("Name 1");
        dec_btn.setText("Example");
    
        setVisible(true);
        return mainFrame;
    }
    

    */

    public static void main(String[] args){
        //Initialize the constructor...
        Password ps = new Password();
    }
    

    }

    这将使您了解如何在 java 上实现 GUI。

    【讨论】:

    • 创建两个面板然后将第一个添加到第二个的具体意义何在?但无论如何,谢谢你,这很好用,老实说,这似乎比我的方法更合乎逻辑:D
    • 主要是订购。面板 1 将是其中的元素并对其进行排序的面板,而面板 2 将是直接添加到框架中的面板。
    • 如果您想显示另一个屏幕,在面板之间切换会更容易。只需删除第一个面板并将新面板添加到面板 2。希望它有所帮助!
    【解决方案2】:

    panel1 从未定义为新的 JPanel(),因此它为 null,并且您的所有其他组件(pwSelection、title 等)从未添加到 panel1。

    【讨论】:

    • 好的,我添加了:protected JPanel panel1 = new JPanel();并为所有组件添加了 panel1.add(...) 。但是现在 Frame 不再显示任何组件了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2017-11-10
    • 2015-03-14
    • 1970-01-01
    • 2014-11-30
    • 2015-06-16
    • 1970-01-01
    相关资源
    最近更新 更多