您是java的新手,并且一直在尝试为大学项目创建表单。 我处于早期开发阶段,我的文本字段工作,但我的标签不可见。 我的文本区域也很大,拉伸了整个Jframe。 这是我的醋栗代码 当前一个类中的用户登录名为(java.java。)此类被调用(AddProperty.java)
时,此Jframe会自动打开package Main;
/**
*
* @author Graeme
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
public class AddProperty
{
public void gui()
{
JFrame frame = new JFrame("AddPropertyFrame");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
JPanel panel = new JPanel();
frame.add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel HouseNumber = new JLabel();
panel.add(HouseNumber);
JTextField HouseNumber1 = new JTextField();
panel.add(HouseNumber1);
JLabel HousePrice = new JLabel();
panel.add(HousePrice);
JTextField HousePrice1 = new JTextField();
panel.add(HousePrice1);
JLabel HouseType = new JLabel();
panel.add(HouseType);
JTextField HouseType1 = new JTextField();
panel.add(HouseType1);
JButton submit = new JButton("Submit");
panel.add(submit);
submit.addActionListener(new Action());
}
static class Action implements ActionListener{
public void actionPerformed (ActionEvent e)
{
JFrame frame2 = new JFrame("Submitted");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label = new JLabel("You Have Submitted a New Property");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
}
谢谢,我们非常感谢任何建议。
答案 0 :(得分:1)
我认为JLabel不可见,因为它们没有显示在其中的东西......
尝试在调用构造函数时为JLabel赋值...这只是我的猜测...
new JLabel("House Nummber");
答案 1 :(得分:1)
请参阅代码中的注释。
/**
*
* @author Graeme
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
public class AddProperty
{
public void gui()
{
JFrame frame = new JFrame("AddPropertyFrame");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// having to set sizes of components is rare, and often a sign
// of problems with layouts.
//frame.setSize(800,600);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
// make it big like the original
panel.setBorder(new EmptyBorder(100,20,100,20));
frame.add(panel);
//panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel HouseNumber = new JLabel("A");
panel.add(HouseNumber);
JTextField HouseNumber1 = new JTextField(10);
panel.add(HouseNumber1);
JLabel HousePrice = new JLabel("B");
panel.add(HousePrice);
JTextField HousePrice1 = new JTextField(10);
panel.add(HousePrice1);
JLabel HouseType = new JLabel("C");
panel.add(HouseType);
JTextField HouseType1 = new JTextField(5);
panel.add(HouseType1);
JButton submit = new JButton("Submit");
panel.add(submit);
submit.addActionListener(new Action());
// tell the GUI to assume its natural (minimum) size.
frame.pack();
}
static class Action implements ActionListener{
public void actionPerformed (ActionEvent e)
{
// this should probably be a modal JDialog or JOptionPane.
JFrame frame2 = new JFrame("Submitted");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label = new JLabel("You Have Submitted a New Property");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AddProperty addProperty = new AddProperty();
addProperty.gui();
}
});
}
}
答案 2 :(得分:1)
未显示JLabel的原因是因为没有显示任何值。
尝试在调用其构造函数时添加一个值,例如;
new JLabel("House Nummber");