JFrame 表单创建(多行、多列和输入)
JFrame form creation (multiple rows, columns, and inputs)
我想实现一个遵循以下照片流程的 JFrame:
我想写一个有两个介绍文本的框架,每个问题一行,同一行有一个输入框。我想要 10 行,然后在底部有一个按钮来完成对话框。
我已经尝试实现这个,但我无法通过第一行。至少有人能指出我正确的方向吗?具有前 4 行和按钮的代码可以让我自己完成。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class GUI_Short_Scale extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
JDialog d1;
public int number;
JButton cont;
JTextField tf;
public GUI_Short_Scale()
{
createAndShowGUI();
}
public int getNumber()
{
return this.number;
}
private void createAndShowGUI()
{
// Must be called before creating JDialog for
// the desired effect
JDialog.setDefaultLookAndFeelDecorated(true);
// A perfect constructor, mostly used.
// A dialog with current frame as parent
// a given title, and modal
d1 = new JDialog(this,"Short Scale",true);
// Set size
d1.setSize(400,400);
d1.setLocationRelativeTo(null); // *** this will center your app ***
d1.setLayout(new FlowLayout());
cont = new JButton("Continue");
cont.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(isNumber(tf.getText()))
{
// everything worked out just fine
number = Integer.valueOf(tf.getText());
d1.dispatchEvent(new WindowEvent(d1, WindowEvent.WINDOW_CLOSING));
}
else
{
// create a jframe
JFrame frame = new JFrame("Error");
// show a joptionpane dialog using showMessageDialog
JOptionPane.showMessageDialog(frame,"The input you gave does not look like a number. Please try again.");
}
}
});
tf = new JTextField(20);
d1.add(new JLabel("Tetris Intro"));
d1.add(tf);
d1.add(cont);
d1.setVisible(true);
}
private boolean isNumber(String s)
{
try
{
Integer.valueOf(s);
}
catch(NumberFormatException ne)
{
return false;
}
// is a number
return true;
}
}
也许像...
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextArea intro = new JTextArea(4, 20);
JTextArea moreText = new JTextArea(4, 20);
JLabel question = new JLabel("Question 1");
JTextField answer = new JTextField(20);
JButton btn = new JButton("Done");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JScrollPane(intro), gbc);
gbc.gridy++;
add(new JScrollPane(moreText), gbc);
gbc.gridy++;
gbc.insets = new Insets(20, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
add(question, gbc);
gbc.gridx++;
add(answer, gbc);
gbc.insets = new Insets(80, 2, 2, 2);
gbc.gridy++;
gbc.gridx = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
}
}
或
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextArea intro = new JTextArea(4, 20);
JTextArea moreText = new JTextArea(4, 20);
JLabel question = new JLabel("Question 1");
JTextField answer = new JTextField(20);
JButton btn = new JButton("Done");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JScrollPane(intro), gbc);
gbc.gridy++;
add(new JScrollPane(moreText), gbc);
gbc.gridwidth = 1;
gbc.gridy++;
gbc.insets = new Insets(20, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
add(question, gbc);
gbc.gridx++;
add(answer, gbc);
gbc.insets = new Insets(80, 2, 2, 2);
gbc.gridy++;
gbc.gridx = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
}
}
查看 Laying Out Components Within a Container and How to Use GridBagLayout 了解更多详情
but could you drop in the code which has two question lines?
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextArea intro = new JTextArea(4, 20);
JTextArea moreText = new JTextArea(4, 20);
JLabel question1 = new JLabel("Question 1");
JTextField answer1 = new JTextField(20);
JLabel question2 = new JLabel("Question 2");
JTextField answer2 = new JTextField(20);
JButton btn = new JButton("Done");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JScrollPane(intro), gbc);
gbc.gridy++;
add(new JScrollPane(moreText), gbc);
gbc.gridy++;
gbc.insets = new Insets(20, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
add(question1, gbc);
gbc.gridx++;
add(answer1, gbc);
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridy++;
gbc.gridx = 0;
add(question2, gbc);
gbc.gridx++;
add(answer2, gbc);
gbc.insets = new Insets(80, 2, 2, 2);
gbc.gridy++;
gbc.gridx = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
}
}
我想实现一个遵循以下照片流程的 JFrame:
我想写一个有两个介绍文本的框架,每个问题一行,同一行有一个输入框。我想要 10 行,然后在底部有一个按钮来完成对话框。
我已经尝试实现这个,但我无法通过第一行。至少有人能指出我正确的方向吗?具有前 4 行和按钮的代码可以让我自己完成。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class GUI_Short_Scale extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
JDialog d1;
public int number;
JButton cont;
JTextField tf;
public GUI_Short_Scale()
{
createAndShowGUI();
}
public int getNumber()
{
return this.number;
}
private void createAndShowGUI()
{
// Must be called before creating JDialog for
// the desired effect
JDialog.setDefaultLookAndFeelDecorated(true);
// A perfect constructor, mostly used.
// A dialog with current frame as parent
// a given title, and modal
d1 = new JDialog(this,"Short Scale",true);
// Set size
d1.setSize(400,400);
d1.setLocationRelativeTo(null); // *** this will center your app ***
d1.setLayout(new FlowLayout());
cont = new JButton("Continue");
cont.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(isNumber(tf.getText()))
{
// everything worked out just fine
number = Integer.valueOf(tf.getText());
d1.dispatchEvent(new WindowEvent(d1, WindowEvent.WINDOW_CLOSING));
}
else
{
// create a jframe
JFrame frame = new JFrame("Error");
// show a joptionpane dialog using showMessageDialog
JOptionPane.showMessageDialog(frame,"The input you gave does not look like a number. Please try again.");
}
}
});
tf = new JTextField(20);
d1.add(new JLabel("Tetris Intro"));
d1.add(tf);
d1.add(cont);
d1.setVisible(true);
}
private boolean isNumber(String s)
{
try
{
Integer.valueOf(s);
}
catch(NumberFormatException ne)
{
return false;
}
// is a number
return true;
}
}
也许像...
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextArea intro = new JTextArea(4, 20);
JTextArea moreText = new JTextArea(4, 20);
JLabel question = new JLabel("Question 1");
JTextField answer = new JTextField(20);
JButton btn = new JButton("Done");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JScrollPane(intro), gbc);
gbc.gridy++;
add(new JScrollPane(moreText), gbc);
gbc.gridy++;
gbc.insets = new Insets(20, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
add(question, gbc);
gbc.gridx++;
add(answer, gbc);
gbc.insets = new Insets(80, 2, 2, 2);
gbc.gridy++;
gbc.gridx = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
}
}
或
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextArea intro = new JTextArea(4, 20);
JTextArea moreText = new JTextArea(4, 20);
JLabel question = new JLabel("Question 1");
JTextField answer = new JTextField(20);
JButton btn = new JButton("Done");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JScrollPane(intro), gbc);
gbc.gridy++;
add(new JScrollPane(moreText), gbc);
gbc.gridwidth = 1;
gbc.gridy++;
gbc.insets = new Insets(20, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
add(question, gbc);
gbc.gridx++;
add(answer, gbc);
gbc.insets = new Insets(80, 2, 2, 2);
gbc.gridy++;
gbc.gridx = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
}
}
查看 Laying Out Components Within a Container and How to Use GridBagLayout 了解更多详情
but could you drop in the code which has two question lines?
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JTextArea intro = new JTextArea(4, 20);
JTextArea moreText = new JTextArea(4, 20);
JLabel question1 = new JLabel("Question 1");
JTextField answer1 = new JTextField(20);
JLabel question2 = new JLabel("Question 2");
JTextField answer2 = new JTextField(20);
JButton btn = new JButton("Done");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JScrollPane(intro), gbc);
gbc.gridy++;
add(new JScrollPane(moreText), gbc);
gbc.gridy++;
gbc.insets = new Insets(20, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
add(question1, gbc);
gbc.gridx++;
add(answer1, gbc);
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridy++;
gbc.gridx = 0;
add(question2, gbc);
gbc.gridx++;
add(answer2, gbc);
gbc.insets = new Insets(80, 2, 2, 2);
gbc.gridy++;
gbc.gridx = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
}
}