gpt4 book ai didi

Java GUI 为空白

转载 作者:行者123 更新时间:2023-11-29 04:43:16 29 4
gpt4 key购买 nike

我开始使用 net-beans GUI builder 构建 GUI,从那时起我决定尝试在没有 GUI builder 的情况下重建它,因为我认为这可能是不好的做法,并不能真正让我通过 swing 学习任何东西。

我试图开始重建 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");

}
}

一如既往地感谢您的帮助

最佳答案

请进行以下更改并查看结果:

第 1 步:

插入以下行作为 Form1.java 的方法 private void initComponents() 的最后语句:

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 步:

Form1.java 的现有 main 方法替换为以下方法:

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 步:

Form2.java中搜索jLabel3.setText(""),替换为jLabel3.setText("Output")

第 5 步:

插入以下行作为 Form2.java 的方法 private void initComponents() 的最后语句:

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 在我的系统上的输出是:

enter image description here

Form2.java 在我的系统上的输出是:

enter image description here

关于Java GUI 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323372/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com