私はこの割り当てのためにコードを動作させていますが、適切に見えるようにする方法がわかりません。私は少し困惑しています。コンテナに追加された 5 つの領域すべてで Border Layout を使用する必要があります。正しい方向に向ける必要があるだけです...ありがとう。写真は載せられませんが・・・こんな感じです。
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)
{
}
}
}