[英]Java GUI Layout Issue
I have the code working for this assignment, but can't figure out how to get it to look properly. 我有代码工作这个任务,但无法弄清楚如何让它看起来正确。 I am a bit baffled.
我有点困惑。 Need to use a Border Layout with all five regions added to the container.
需要使用边框布局,将所有五个区域添加到容器中。 Just need to be pointed in the right direction...thanks.
只需要指出正确的方向......谢谢。 I can't post a pic...but it is something like this...
我不能发布图片...但它是这样的......
Sum Numbers(Centered)
Text box 1
Text box 2
Text box 3
Result is: sum of numbers
Button 1(Sum Numbers)Button 2(Close)
Code: 码:
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)
{
}
}
}
Ok here is how you go about doing it: 好的,这是你如何去做:
JFrame
and set its layout to BorderLayout
. JFrame
并将其布局设置为BorderLayout
。 JLabel
with text set to "Add Numbers" and add to the JFrame
it using BorderLayout.NORTH
JLabel
,并使用BorderLayout.NORTH
将其添加到JFrame
JPanel
, set its layout to BorderLayout
. JPanel
,将其布局设置为BorderLayout
。 JTextField
, add them to the JPanel
using BorderLayout.NORTH
, BorderLayout.CENTER
and BorderLayout.SOUTH
JTextField
,使用BorderLayout.NORTH
, BorderLayout.CENTER
和BorderLayout.SOUTH
将它们添加到JPanel
JPanel
. JPanel
。 JLabel
to display "Total is 60". JLabel
以显示“Total is 60”。 Also create the two JButtons
. JButtons
。 JPanel
of step 5 to BorderLayout
. JPanel
布局设置为BorderLayout
。 Add the JLabel
using BorderLayout.NORTH
. BorderLayout.NORTH
添加JLabel
。 JPanel
with FlowLayout
, add the two buttons. FlowLayout
创建另一个JPanel
,添加两个按钮。 Then add this JPanel
to the JPanel
in step 5 with BorderLayout.SOUTH
. BorderLayout.SOUTH
将此JPanel
添加到JPanel
。 JPanels
to your JFrame
. JPanels
添加到JFrame
。 Here is an example for you. 这是一个例子。 I have used
GridBagLayout
because it's very flexible. 我使用了
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();
}
}
And never do that if you use MouseListener
only for clicks : 如果您仅将
MouseListener
用于点击,那么永远不要这样做:
jbtAdd.addMouseListener(new List_ButtonADD());
jbtClear.addMouseListener(new List_ButtonCLEAR());
MouseListener
uses for another purposes. MouseListener
用于其他目的。 Instead of that use ActionListener
it uses for button actions. 而不是使用
ActionListener
它用于按钮操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.