我有代码正在为此作业工作,但无法弄清楚如何使其正常显示。我有点困惑。需要使用边框布局,将所有五个区域添加到容器中。只需要指出正确的方向......谢谢。我不能发布图片...但是它就是这样......
Sum Numbers(Centered)
Text box 1
Text box 2
Text box 3
Result is: sum of numbers
Button 1(Sum Numbers)Button 2(Close)
代码:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
public class Calculator extends JFrame {
// Declare GUI components
private JTextField jtfFirst;
private JTextField jtfSecond;
private JTextField jtfThird;
private JTextField jtfResult;
private JLabel jlblTitle;
private JLabel jlblFirst;
private JLabel jlblSecond;
private JLabel jlblThird;
private JLabel jlblResult;
private JPanel entryPanel;
private JPanel buttonPanel;
private JButton jbtAdd;
private JButton jbtClear;
// main method to instantiate and customize frame
public static void main(String[] args)
{
Vector<String> myVector=new Vector<String>();
Calculator frame = new Calculator();
frame.setTitle( "Assignment 5");
int windowWidth = 300;
int windowHeight = 200;
frame.setSize( 350, 150);
frame.setLocation(400, 400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
// GUI constructor
public Calculator()
{
// a bottom JPanel to place everything on.
//JPanel totalGUI = new JPanel();
//totalGUI.setLayout(null);
JLabel textLabel = new JLabel("Add Numbers", JLabel.CENTER);
textLabel.setFont(new Font("Ariel",Font.PLAIN,24));
//textLabel.setPreferredSize(new Dimension(300, 100));
jlblResult = new JLabel( "Total is: ", JLabel.LEFT );
jtfFirst = new JTextField(9);
jtfSecond = new JTextField(9);
jtfThird = new JTextField(9);
jtfResult = new JTextField(6);
entryPanel = new JPanel();
// make result text field uneditable
jtfResult.setEditable( false );
// Set layout manager of panel
entryPanel.setLayout( new GridLayout(6,1,2,2));
// add GUI components to panel
entryPanel.add(textLabel );
entryPanel.add( jtfFirst );
entryPanel.add( jtfSecond );
entryPanel.add( jtfThird );
entryPanel.add( jlblResult);
entryPanel.add( jtfResult );
// add entryPanel to frame
add(entryPanel, BorderLayout.CENTER);
// Instantiate GUI components for bottom of frame
jbtAdd = new JButton( "Show Total" );
jbtClear = new JButton( "Clear" );
buttonPanel = new JPanel();
// add buttons to panel
buttonPanel.add( jbtAdd );
buttonPanel.add( jbtClear );
// add buttonPanel to frame
add( buttonPanel, BorderLayout.SOUTH);
//Event Handler
jbtAdd.addMouseListener(new List_ButtonADD());
jbtClear.addMouseListener(new List_ButtonCLEAR());
}
private class List_ButtonADD implements MouseListener
{
public void mouseClicked(MouseEvent event)
{
double x=Double.parseDouble(jtfFirst.getText());
double y=Double.parseDouble(jtfSecond.getText());
double z=Double.parseDouble(jtfThird.getText());
jtfResult.setText(String.valueOf(x+y+z));
public void mousePressed(MouseEvent event)
{
}
public void mouseReleased(MouseEvent event)
{
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
}
}
private class List_ButtonCLEAR implements MouseListener
{
public void mouseClicked(MouseEvent event)
{
System.exit (0);
}
public void mousePressed(MouseEvent event)
{
}
public void mouseReleased(MouseEvent event)
{
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
}
}
}
答案 0 :(得分:2)
好的,你可以这样做:
JFrame
并将其布局设置为BorderLayout
。JLabel
,文字设置为“添加数字”,然后使用JFrame
BorderLayout.NORTH
JPanel
,将其布局设置为BorderLayout
。 JTextField
,使用JPanel
,BorderLayout.NORTH
和BorderLayout.CENTER
BorderLayout.SOUTH
JPanel
。 JLabel
以显示“总计为60”。同时创建两个JButtons
。 JPanel
的布局设置为BorderLayout
。使用JLabel
添加BorderLayout.NORTH
。 JPanel
创建另一个FlowLayout
,添加两个按钮。然后使用JPanel
将此JPanel
添加到步骤5中的BorderLayout.SOUTH
。 JPanels
添加到您的JFrame
。答案 1 :(得分:2)
以下是您的示例。我使用GridBagLayout
因为它非常灵活。
public class ExampleFrame extends JFrame {
private JTextField jtfFirst;
private JTextField jtfSecond;
private JTextField jtfThird;
private JTextField jtfResult;
public ExampleFrame() {
super();
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
add(new JLabel("Sum Numbers"),c);
c.gridwidth = 1;
c.anchor = GridBagConstraints.WEST;
c.gridy = 1;
add(new JLabel("Text1"),c);
c.gridy =2;
add(new JLabel("Text2"),c);
c.gridy =3;
add(new JLabel("Text3"),c);
c.gridy =4;
add(new JLabel("Result"),c);
c.gridy =5;
JButton b1 = new JButton("b1");
b1.addActionListener(getActionB1Listener());
add(b1,c);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
add(jtfFirst = new JTextField(),c);
c.gridy = 2;
add(jtfSecond = new JTextField(),c);
c.gridy = 3;
add(jtfThird = new JTextField(),c);
c.gridy = 4;
add(jtfResult = new JTextField(),c);
JButton b2 = new JButton("b2");
b2.addActionListener(getActionB2Listener());
c.fill = GridBagConstraints.NONE;
c.weightx = 0;
c.gridy = 5;
add(b2,c);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private ActionListener getActionB1Listener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("b1 clicked");
}
};
}
private ActionListener getActionB2Listener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("b2 clicked");
}
};
}
public static void main(String... s) {
new ExampleFrame();
}
}
如果您仅对点击次数使用MouseListener
,那么永远不会这样做:
jbtAdd.addMouseListener(new List_ButtonADD());
jbtClear.addMouseListener(new List_ButtonCLEAR());
MouseListener
用于其他目的。而不是使用它ActionListener
,它用于按钮操作。