答案 0 :(得分:1)
这就是我解决的方式
public class firstSwingForm {
private JPanel config;
private JTextField startTxt;
private JTextField dogTextField;
private JPanel mainPanel;
private JTextField a5TextField;
private JButton startBtn;
private static firstSwingForm instance;
public static void main(String args[]) {
JFrame frame = new JFrame("App");
instance = new firstSwingForm();
frame.setContentPane(instance.mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
instance.startBtn.setText("text can be set");
答案 1 :(得分:0)
问题是您的private JButton startBtn
必须声明为静态才能在主方法private static JButton startBtn;
在调用主体上的任何内容之前,还应将其实例化为主体中的新对象:startBtn = new JButton(...);
同样值得注意的是,按照惯例,您的班级名称应为FirstSwingForm
,
并仔细考虑这些实例变量是否将在其他地方使用或是否可以在main中定义。