有一个完整的编译代码,按照我的书中的示例,现在我迷失了尝试更改JPanel的设置,以显示两列测试分数及其重量的文本输入...显示的是测试和重量叠在一起......
我的代码如下:
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();
}
}
任何帮助表示赞赏
答案 0 :(得分:4)
你想要5行而不是10行。new GridLayout(5, 4)
会起作用,但更简单的是对行使用0 - 这意味着根据需要添加新行:
pane.setLayout(new GridLayout(0, 4));
与问题无关,但不建议指定窗口大小。而是在显示窗口之前使用pack()
。这导致窗口大小自动确定,适合内容窗格的首选大小。
...
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();
}
});
}