Java Swing JComboBox/Button编辑参数
Java Swing JComboBox/ Button edit parameters
我对在 Java 中使用 GUI 还很陌生。我尝试了几件事,例如,在 运行 方法中使用以下命令向 JComboBox 添加新条目或更改 JButtons 标题:
pwSelection.addItem("Name 1");
dec_btn.setText("Example");
受保护的 JComboBox pwSelection = new JComboBox(内容);
不幸的是,none 一启动程序就显示出任何效果。
布局是使用 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();
}
}
panel1 从未定义为新的 JPanel(),因此它为空,并且您的所有其他组件——pwSelection、标题等——从未添加到 panel1。
您没有向框架添加任何面板,也没有初始化任何属性...
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。
我对在 Java 中使用 GUI 还很陌生。我尝试了几件事,例如,在 运行 方法中使用以下命令向 JComboBox 添加新条目或更改 JButtons 标题:
pwSelection.addItem("Name 1");
dec_btn.setText("Example");
受保护的 JComboBox pwSelection = new JComboBox(内容);
不幸的是,none 一启动程序就显示出任何效果。 布局是使用 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();
}
}
panel1 从未定义为新的 JPanel(),因此它为空,并且您的所有其他组件——pwSelection、标题等——从未添加到 panel1。
您没有向框架添加任何面板,也没有初始化任何属性...
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。