Java 为什么我需要最小化然后重新打开才能加载?
Java Why do I need to minimize then reopen to load?
我制作了一个简单的 java 程序来放入任意数量的按钮,但我必须将 window 最小化,然后再将其重新打开以使按钮实际出现,求助请?这是代码:
package ButtonTest;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Btest {
public int additive = 20;
private JFrame frame;
private JTextField txtNumber;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Btest window = new Btest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Btest() {
initialize();
}
private void initialize() {
frame = new JFrame("btest");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblBtncount = new JLabel("BtnCount");
lblBtncount.setBounds(24, 39, 61, 16);
frame.getContentPane().add(lblBtncount);
txtNumber = new JTextField();
txtNumber.setText("Number");
txtNumber.setBounds(99, 33, 134, 28);
frame.getContentPane().add(txtNumber);
txtNumber.setColumns(10);
JButton btnSbmt = new JButton("Sbmt");
btnSbmt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String numba = txtNumber.getText();
int number = Integer.parseInt(numba);
for(int amntB=1; amntB<number+1; amntB++){
String amntS = Integer.toString(amntB);
JButton button = new JButton(amntS);
button.setBounds(327, 6+additive, 117, 29);
frame.getContentPane().add(button);
additive += 20;
}
}
});
btnSbmt.setBounds(155, 202, 117, 29);
frame.getContentPane().add(btnSbmt);
}
}
感谢您抽出宝贵时间查看此内容,也许可以给我一个答案?谢谢!
What layout do you suggest I use?
我会使用 GridBagLayout
,但您可以使用其他...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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;
public class Btest {
public int additive = 20;
private JFrame frame;
private JTextField txtNumber;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Btest window = new Btest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Btest() {
initialize();
}
private void initialize() {
frame = new JFrame("btest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
JLabel lblBtncount = new JLabel("BtnCount");
frame.getContentPane().add(lblBtncount, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
txtNumber = new JTextField();
txtNumber.setText("Number");
frame.getContentPane().add(txtNumber, gbc);
txtNumber.setColumns(10);
JButton btnSbmt = new JButton("Sbmt");
btnSbmt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String numba = txtNumber.getText();
int number = Integer.parseInt(numba);
for (int amntB = 1; amntB < number + 1; amntB++) {
String amntS = Integer.toString(amntB);
JButton button = new JButton(amntS);
button.setBounds(327, 6 + additive, 117, 29);
frame.getContentPane().add(button);
additive += 20;
}
}
});
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(40, 4, 4, 4);
frame.getContentPane().add(btnSbmt, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
}
}
我制作了一个简单的 java 程序来放入任意数量的按钮,但我必须将 window 最小化,然后再将其重新打开以使按钮实际出现,求助请?这是代码:
package ButtonTest;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Btest {
public int additive = 20;
private JFrame frame;
private JTextField txtNumber;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Btest window = new Btest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Btest() {
initialize();
}
private void initialize() {
frame = new JFrame("btest");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblBtncount = new JLabel("BtnCount");
lblBtncount.setBounds(24, 39, 61, 16);
frame.getContentPane().add(lblBtncount);
txtNumber = new JTextField();
txtNumber.setText("Number");
txtNumber.setBounds(99, 33, 134, 28);
frame.getContentPane().add(txtNumber);
txtNumber.setColumns(10);
JButton btnSbmt = new JButton("Sbmt");
btnSbmt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String numba = txtNumber.getText();
int number = Integer.parseInt(numba);
for(int amntB=1; amntB<number+1; amntB++){
String amntS = Integer.toString(amntB);
JButton button = new JButton(amntS);
button.setBounds(327, 6+additive, 117, 29);
frame.getContentPane().add(button);
additive += 20;
}
}
});
btnSbmt.setBounds(155, 202, 117, 29);
frame.getContentPane().add(btnSbmt);
}
}
感谢您抽出宝贵时间查看此内容,也许可以给我一个答案?谢谢!
What layout do you suggest I use?
我会使用 GridBagLayout
,但您可以使用其他...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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;
public class Btest {
public int additive = 20;
private JFrame frame;
private JTextField txtNumber;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Btest window = new Btest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Btest() {
initialize();
}
private void initialize() {
frame = new JFrame("btest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
JLabel lblBtncount = new JLabel("BtnCount");
frame.getContentPane().add(lblBtncount, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
txtNumber = new JTextField();
txtNumber.setText("Number");
frame.getContentPane().add(txtNumber, gbc);
txtNumber.setColumns(10);
JButton btnSbmt = new JButton("Sbmt");
btnSbmt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String numba = txtNumber.getText();
int number = Integer.parseInt(numba);
for (int amntB = 1; amntB < number + 1; amntB++) {
String amntS = Integer.toString(amntB);
JButton button = new JButton(amntS);
button.setBounds(327, 6 + additive, 117, 29);
frame.getContentPane().add(button);
additive += 20;
}
}
});
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(40, 4, 4, 4);
frame.getContentPane().add(btnSbmt, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
}
}