我想在 JFrame 中的特定坐标上放置一个 Jbutton。我曾尝试使用 setLocation 和 setBounds 但它们都不起作用。我想我肯定做错了什么。我是 Java 新手,并尝试过搜索
我的输出:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BingoHelper extends JFrame implements WindowListener, ActionListener{
JTextField text = new JTextField(10);
private JButton b; {
b = new JButton("Click to enter name");
}
public void actionPerformed (ActionEvent e) {
String fn = JOptionPane.showInputDialog("Username:");
String sn = JOptionPane.showInputDialog("Password:");
JOptionPane.showMessageDialog(null, "Welcome " + fn + " " + sn + ".", "", JOptionPane.INFORMATION_MESSAGE);
text.setText(fn + " " + sn);
b.setVisible(false);
text.setVisible(true);
text.setBounds(100,20,49,90);
b.setBounds(100,90,22,30);
}
public BingoHelper(){
super("BINGO");
setLayout(new FlowLayout());
add(b);
add(text);
text.setVisible(false);
b.setVisible(true);
b.setBounds(0,0,220,30);
b.addActionListener(this);
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}