gpt4 book ai didi

java - JFrame 表单创建(多行、多列和输入)

转载 作者:行者123 更新时间:2023-12-02 04:50:23 24 4
gpt4 key购买 nike

我想实现一个遵循以下照片流程的 JFrame:

enter image description here

我想编写一个框架,其中有两个介绍文本,每个问题一行,同一行有一个输入框。我想要 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;
}
}

最佳答案

也许像...

Layout 01

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);

}

}

或者

Layout02

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 ContainerHow 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);

}

}

关于java - JFrame 表单创建(多行、多列和输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29293957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com