gpt4 book ai didi

Java GUI完全丢失

转载 作者:行者123 更新时间:2023-12-01 18:17:55 26 4
gpt4 key购买 nike

有一个完整的编译代码,按照我书中的示例进行操作,现在我迷失了尝试更改 JPanel 的设置以显示 2 列用于四个测试分数及其权重的文本输入...显示的是测试和重量堆叠在一起......

我的代码如下:

    import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class WeightedAverage extends JFrame
{ // Defines all variables
private JLabel Test1, Test2,
Test3, Test4, Weight1, Weight2, Weight3, Weight4, ansa;

private JTextField testa, testb, testc, testd, weighta, weightb, weightc, weightd, ans;

private JButton calculateB, exitB;

private CalculateButtonHandler cbHandler;

private ExitButtonHandler ebHandler;
// Sets programs dimensions
private static final int WIDTH = 500;
private static final int HEIGHT = 600;

public WeightedAverage()
{
// Create labels of text fields
Test1 = new JLabel("First Test Score: ",
SwingConstants.RIGHT);
Weight1 = new JLabel("And It's Weight: ",
SwingConstants.RIGHT);

Test2 = new JLabel("Second Test Score: ",
SwingConstants.RIGHT);
Weight2 = new JLabel("And It's Weight: ",
SwingConstants.RIGHT);

Test3 = new JLabel("Third Test Score: ",
SwingConstants.RIGHT);
Weight3 = new JLabel("And It's Weight: ",
SwingConstants.RIGHT);

Test4 = new JLabel("Fourth Test Score: ",
SwingConstants.RIGHT);
Weight4 = new JLabel("And It's Weight: ",
SwingConstants.RIGHT);


ansa = new JLabel("You may not want to see this: ",
SwingConstants.RIGHT);


//Create input text fields
testa = new JTextField(10);
weighta = new JTextField(10);
testb = new JTextField(10);
weightb = new JTextField(10);
testc = new JTextField(10);
weightc = new JTextField(10);
testd = new JTextField(10);
weightd = new JTextField(10);




//create Calculate Button
calculateB = new JButton("Crack that whip!");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);

//Create Exit Button
exitB = new JButton("Begone!");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);

//Creat Answer Field
ans = new JTextField(10);

//Set the title of the window
setTitle("Yes master");

//Get the container
Container pane = getContentPane();

//Set the layout
pane.setLayout(new GridLayout(10,4));

//Place all items created
pane.add(Test1);
pane.add(testa);
pane.add(Weight1);
pane.add(weighta);

pane.add(Test2);
pane.add(testb);
pane.add(Weight2);
pane.add(weightb);

pane.add(Test3);
pane.add(testc);
pane.add(Weight3);
pane.add(weightc);

pane.add(Test4);
pane.add(testd);
pane.add(Weight4);
pane.add(weightd);

pane.add(calculateB);
pane.add(exitB);
pane.add(ansa);
pane.add(ans);



//set the size of the window and display it
setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double t1, t2, t3, t4, w1, w2, w3, w4, avg1, avg2, avg3, avg4, favg;

t1 = Double.parseDouble(testa.getText());
w1 = Double.parseDouble(weighta.getText());
t2 = Double.parseDouble(testb.getText());
w2 = Double.parseDouble(weightb.getText());
t3 = Double.parseDouble(testc.getText());
w3 = Double.parseDouble(weightc.getText());
t4 = Double.parseDouble(testd.getText());
w4 = Double.parseDouble(weightd.getText());


// Calculations Required to output weighted average
avg1 = t1 * w1;
avg2 = t2 * w2;
avg3 = t3 * w3;
avg4 = t4 * w4;
favg = avg1 + avg2 + avg3 + avg4;
// Outputs the weighted Average
ans.setText("" + favg);

}
}

private class ExitButtonHandler implements ActionListener
{
// Exits the program
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

public static void main(String[] args)
{
WeightedAverage weight = new WeightedAverage();
}
}

感谢任何帮助

最佳答案

您想要 5 行而不是 10 行。new GridLayout(5, 4) 可以,但更简单的是使用 0 作为行 - 这意味着根据需要添加新行:

pane.setLayout(new GridLayout(0, 4));

与问题无关,但不建议指定窗口大小。请在显示窗口之前使用 pack()。这会导致自动确定窗口大小,以适合内容 Pane 的首选大小。

...
pack();
setVisible(true);

最后,swing 是单线程的,访问和创建 swing 组件只能在 event dispatch thread 中完成。 ,因此您应该创建如下 GUI:

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
WeightedAverage weight = new WeightedAverage();
}
});
}

关于Java GUI完全丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502223/

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