我开始使用net-beans GUI构建器构建GUI,从那以后我决定尝试在没有GUI构建器的情况下重建它,因为我认为它可能是不好的做法并且不能真正让我学习任何东西摇摆。
我试图开始重建GUI但是当我运行该程序时,一个空白的GUI没有任何功能弹出,想知道是否有人可以帮助我为什么这样做?我的程序包含两个用于单独窗口的类(Frames(?))。
我的代码如下:
package com.company;
public class Form1 extends javax.swing.JFrame {
private javax.swing.JButton btnComboBox;
private javax.swing.JComboBox<String> comboOne;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
public Form1() {
initComponents();
}
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
comboOne = new javax.swing.JComboBox<>();
jLabel3 = new javax.swing.JLabel();
btnComboBox = new javax.swing.JButton();
jLabel2.setText("SELECT PRINTER:");
comboOne.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Printer 1", "Printer 2", "Printer 3", "Printer 4"}));
comboOne.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboOneActionPerformed(evt);
}
});
btnComboBox.setText("GO");
btnComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnComboBoxActionPerformed(evt);
}
});
}
private void comboOneActionPerformed(java.awt.event.ActionEvent evt) {
Object selected = comboOne.getSelectedItem().toString();
}
private void btnComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
// Opens a new form, converts the object selected in combobox too a string
// Passes string to openMe on form 2
Form2 f2 = new Form2(comboOne.getSelectedItem().toString());
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Form1().setVisible(true);
}
});
}
}
表格2:
package com.company;
import javax.swing.*;
public class Form2 extends javax.swing.JFrame {
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jta;
public Form2() {
initComponents();
}
public Form2(String message) {
initComponents();
jta.append(" Printer selected: " + message + "\n");
this.setVisible(true);
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jta = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Console");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel3.setText("");
jta.setColumns(20);
jta.setRows(50);
jScrollPane2.setViewportView(jta);
}
public void openMe(String message) {
/*java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Form2().setVisible(true);
}
});*/
System.out.println("----- Console Output ------");
System.out.println("---------------------------");
System.out.println("Printer selected: " + message);
System.out.println("---------------------------");
System.out.println("\n");
System.out.println("\n");
}
}
任何帮助一如既往的
答案 0 :(得分:3)
请进行以下更改并查看结果:
第1步:
将以下行插入private void initComponents()
的方法Form1.java
的最后一句话:
private void initComponents() {
// existing code
this.setLayout(new FlowLayout(java.awt.FlowLayout.CENTER, 15, 15));
this.add(jLabel2);
this.add(comboOne);
this.add(btnComboBox);
}
第2步:
用Form1.java
中的以下内容替换现有定义:
private void btnComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
Form2 f2 = new Form2(comboOne.getSelectedItem().toString());
f2.setSize(250, 300);
f2.setLocationRelativeTo(this);
f2.setVisible(true);
}
第3步:
通过以下方法替换main
的现有Form1.java
方法:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Form1 form1 = new Form1();
form1.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
form1.setSize(300, 200);
form1.setLocationRelativeTo(null);
form1.setVisible(true);
}
});
}
第4步:
在jLabel3.setText("")
中搜索Form2.java
并将其替换为jLabel3.setText("Output")
。
第5步:
将以下行插入private void initComponents()
的方法Form2.java
的最后一句话:
private void initComponents() {
// existing code
this.setLayout(new BorderLayout());
JPanel topPanel=new JPanel();
this.setLayout(new FlowLayout(java.awt.FlowLayout.CENTER, 15, 15));
this.add(jLabel1);
this.add(jLabel3);
this.add(topPanel, BorderLayout.NORTH);
this.add(jScrollPane2, BorderLayout.CENTER);
}
我系统上Form1.java
的输出是:
我系统上Form2.java
的输出是: