我试图在每次单击按钮时创建标签。标签的位置应该改变。每次点击时它都应该在另一个之上。但它不起作用。不创建任何标签。我什至无法从按钮单击监听器代码创建单个 JLabel。但执行发生在最里面的“if”语句内。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ChatClient{
static int y=600;
static JLabel label=null;
public static void main(String args[]) throws Exception{
String defaultMessage = "Enter your message..";
JFrame frame = new JFrame("FireFly");
JTextField text = new JTextField(defaultMessage);
//manipulate the default message in the text field
text.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent ae){
if(text.getText().equals(defaultMessage)){
text.setText("");
}
}
public void focusLost(FocusEvent ae){
if(text.getText().isEmpty()){
text.setText(defaultMessage);
}
}
});
text.setBounds(10,620,295,40);
frame.add(text);
JButton button = new JButton("SEND");
button.setBounds(310,620,80,40);
button.setForeground(Color.WHITE);
button.setBackground(Color.decode("#11A458"));
button.setFocusPainted(false);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(!text.getText().equals(defaultMessage))
{
if(!text.getText().isEmpty()){
label=new JLabel(text.getText());
label.setBounds(10,y,380,20);
y=y-20;
frame.add(label);
}
}
}
});
frame.add(button);
frame.setSize(400,700);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
最佳答案
添加 JLabel 后对容器调用 revalidate 和 repaint 将告诉 GUI 定位新组件并重新绘制容器以使其显示。例如,
public void actionPerformed(ActionEvent ae) {
if (!text.getText().equals(defaultMessage)) {
if (!text.getText().isEmpty()) {
label = new JLabel(text.getText());
label.setBounds(10, y, 380, 20);
y = y - 20;
frame.add(label);
frame.revalidate();
frame.repaint();
}
}
}
就您而言,重新验证并不是绝对必要的,因为您没有使用布局管理器,但包含它仍然是一个好主意,因为您应该将布局管理器用作 null布局是危险且脆弱的。
最好使用 JList。
例如,
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class ChatClient2 extends JPanel {
private static final int VISIBLE_ROW_COUNT = 30;
private DefaultListModel<String> listModel = new DefaultListModel<>();
private JList<String> chatList = new JList<>(listModel);
private SendAction sendAction = new SendAction("Send");
private JTextField textField = new JTextField(20);
private JButton sendButton = new JButton(sendAction);
public ChatClient2() {
chatList.setFocusable(false);
chatList.setVisibleRowCount(VISIBLE_ROW_COUNT);
JScrollPane scrollPane = new JScrollPane(chatList);
textField.setAction(sendAction);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
bottomPanel.add(textField);
bottomPanel.add(sendButton);
setLayout(new BorderLayout());
add(scrollPane);
add(bottomPanel, BorderLayout.PAGE_END);
}
private class SendAction extends AbstractAction {
public SendAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
listModel.add(0, text);
textField.selectAll();
textField.requestFocusInWindow();
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Chat Client2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ChatClient2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
关于java - 使用单个对象在单击按钮时将 JLabel 动态添加到 Java 中的 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44801426/