我在使用Jlabel和setText()方法时遇到了一些问题。通常,setText()应该清除以前的文本,并在按钮单击时替换为新文本,而是将文本相互堆叠。
public class JFrameDemo extends JFrame{
private JButton clearButton;
private JLabel display;
private JButton exitButton;
private JButton go;
private JTextField nameField;
private JLabel name;
public JFrameDemo(){
doEnglish();
}
private void doEnglish(){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
name = new JLabel("Enter Name");
getContentPane().add(name);
name.setBounds(5,30,300,30);
nameField = new JTextField();
getContentPane().add(nameField);
nameField.setBounds(80,33,150,25);
go = new JButton("Go");
getContentPane().add(go);
go.setBounds(5, 65, 50, 30);
go.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
goButtonActionPerformed();
}
});
}
private void goButtonActionPerformed(){
String na = nameField.getText();
display = new JLabel();
display.setText(na);
getContentPane().add(display);
display.setBounds(85,110,400,20);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JFrameDemo().setVisible(true);
}
});
}
}
答案 0 :(得分:1)
null
布局JLabel
的新实例,然后将其添加到内容窗格中......这并不奇怪避免使用null
布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正
请查看What is a Null Pointer Exception, and how do I fix it?了解更多详情......
相反,您应检查display
标签的状态,并仅在需要时创建...
private void goButtonActionPerformed(){
String na = nameField.getText();
if (display == null) {
display = new JLabel();
getContentPane().add(display);
// Get rid of this as soon as you can...
display.setBounds(85,110,400,20);
}
display.setText(na);
}
答案 1 :(得分:0)
每次按下add
按钮,都会向contentPane
添加新的JLabel。所以将代码更改为:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class JFrameDemo extends JFrame{
private JButton clearButton;
private JLabel display = new JLabel();
private JButton exitButton;
private JButton go;
private JTextField nameField;
private JLabel name;
public JFrameDemo(){
doEnglish();
}
private void doEnglish(){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
name = new JLabel("Enter Name");
getContentPane().add(name);
name.setBounds(5,30,300,30);
nameField = new JTextField();
getContentPane().add(nameField);
nameField.setBounds(80,33,150,25);
go = new JButton("Go");
getContentPane().add(go);
go.setBounds(5, 65, 50, 30);
getContentPane().add(display);
display.setBounds(85,110,400,20);
go.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
goButtonActionPerformed();
}
});
}
private void goButtonActionPerformed(){
String na = nameField.getText();
//display = new JLabel();
display.setText(na);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JFrameDemo().setVisible(true);
}
});
}
}