add two JPanels to JFrame - java

I'm trying to add two JPanels to a Jframe, but it seems that they look like one. I'm trying tow stack them on top of each other like this image.
I thinking I may need to look at layout managers? I just need a little nudge in the right direction.
package projectTwo;
import javax.swing.*;
public class checkFrame
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Compose Message");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
checkPanel bob = new checkPanel();
//frame.add(bob);
frame.getContentPane().add(bob);
frame.setResizable(false);
frame.setSize(750, 500);
frame.setVisible(true);
}
}
package projectTwo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class checkPanel extends JPanel implements ActionListener
{
private JPanel entry, display;
private JLabel name, checkAmount, payOrderOf, numPrint, numWords;
private JTextField nameT, checkAmountT;
private JButton Submit;
public checkPanel()
{
entryComponents();
checkDisplay();
}
private void entryComponents(){
name = new JLabel("Name:");
checkAmount = new JLabel("Check Amount:");
nameT = new JTextField(20);
nameT.addActionListener(this);
checkAmountT = new JTextField(20);
checkAmountT.addActionListener(this);
Submit = new JButton("Submit");
Submit.addActionListener(this);
add(name);
add(nameT);
add(checkAmount);
add(checkAmountT);
add(Submit);
setPreferredSize(new Dimension(750, 75));
setBackground(new Color(200,200,200));
}
private void checkDisplay(){
payOrderOf = new JLabel("Pay to the Order of: ");
add(payOrderOf);
setBackground(new Color(220,255,225));
}
public void actionPerformed (ActionEvent event)
{
}
}

You should definitely take a look at layout managers. At the moment you are simply adding JPanels to each other without any specification on where they should be.
You have a few options in this case. You could use a GridLayout, but that leads to all the panels being the same size. If you just want two panels below each other, I would suggest using a BorderLayout. I've adjusted your code as follows:
public class checkPanel extends JPanel implements ActionListener
{
private JPanel entry, display;
private JLabel name, checkAmount, payOrderOf, numPrint, numWords;
private JTextField nameT, checkAmountT;
private JButton Submit;
public checkPanel()
{
this.setPreferredSize(new Dimension(750, 75));
entryComponents();
checkDisplay();
this.setLayout(new BorderLayout());
this.add(entry, BorderLayout.NORTH);
this.add(display, BorderLayout.CENTER);
}
private void entryComponents(){
entry = new JPanel();
// You should specify entry's layout as well FlowLayout are used by default
name = new JLabel("Name:");
checkAmount = new JLabel("Check Amount:");
nameT = new JTextField(20);
nameT.addActionListener(this);
checkAmountT = new JTextField(20);
checkAmountT.addActionListener(this);
Submit = new JButton("Submit");
Submit.addActionListener(this);
entry.add(name);
entry.add(nameT);
entry.add(checkAmount);
entry.add(checkAmountT);
entry.add(Submit);
entry.setBackground(new Color(200,200,200));
}
private void checkDisplay(){
display = new JPanel();
// You should specify display's layout as well FlowLayout are used by default
payOrderOf = new JLabel("Pay to the Order of: ");
display.add(payOrderOf);
display.setBackground(new Color(220,255,225));
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
It is in general a good idea to assign a layout to each JPanel you create. The choice of layout depends on how the panel should function.

using Gridbag Layout can help you a lot I would put a separator between panels as well

Related

Java Swing JComboBox/ Button edit parameters

I am quite new to working with GUI in Java. I was trying several things, for example, adding a new entry to a JComboBox or changing a JButtons caption using the following commands in the run method:
pwSelection.addItem("Name 1");
dec_btn.setText("Example");
protected JComboBox pwSelection = new JComboBox(contents);
Unfortunately, none of it shows any effect as soon as I start the program.
The layout was made with the IntelliJ GUI Form Creator.
It would be great if you could give me any tips or alternative approaches.
package PackMain;
import javax.swing.*;
public class Password {
String[] contents = {"Name 1", "Name 2"};
protected JTextField newAdress;
protected JPanel panel1;
protected JComboBox pwSelection = new JComboBox(contents); // ?
protected JLabel title;
protected JTextField pwOutput;
protected JButton enc_btn;
protected JButton dec_btn;
public JFrame run(){
JFrame mainFrame= new JFrame ("Password");
mainFrame.setContentPane(new Password().panel1);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setSize(400, 200);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
pwSelection.addItem("Name 1");
dec_btn.setText("Example");
return mainFrame;
}
public static void main(String[] args){
new Password().run();
}
}
You're not adding any panel to the frame, also you didn't initialize any of the attributes...
package password;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class Password extends JFrame{
String[] contents = {"Name 1", "Name 2"};
protected JTextField newAdress;
protected JPanel panel1, panel2; //2 panels for a more organized approach...
protected JComboBox pwSelection;// ?
protected JTextField pwOutput;
protected JButton enc_btn;
protected JButton dec_btn;
public Password(){ //Constructor of the class, initialize stuff here...
super("Shinny title");
//1. Initialize all your atributtes...
newAdress = new JTextField();
panel1 = new JPanel();
panel2 = new JPanel();
pwSelection = new JComboBox(contents);
pwOutput= new JTextField();
enc_btn = new JButton("First button");
dec_btn = new JButton("Second Button");
//Set layouts..
panel1.setLayout(new GridLayout(3,1));
panel2.setLayout(new FlowLayout());
//Add items to panel 1...
panel1.add(new JLabel("Adress:")); //A more simple aproach for creating a label...
panel1.add(newAdress);
panel1.add(pwSelection);
panel1.add(enc_btn);
panel1.add(dec_btn);
panel1.add(pwOutput);
//Add items of panel 1 to panel 2...
panel2.add(panel1);
add(panel2);
setSize(300,300);
setVisible(true);
//Proper way of stop the application at closing...
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
//This is not necessary as everything should be implemented in the constructor.
/*
public JFrame run(){
JFrame mainFrame= new JFrame ("Password");
mainFrame.setContentPane(new Password().panel1);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setSize(400, 200);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
pwSelection.addItem("Name 1");
dec_btn.setText("Example");
setVisible(true);
return mainFrame;
}
*/
public static void main(String[] args){
//Initialize the constructor...
Password ps = new Password();
}
}
This will give you an idea of how the GUI on java can be implemented.
panel1 is never defined as a new JPanel(), so it's null, and all of your other components -- pwSelection, title, etc. -- have never been added to panel1.

How can I get the contents of my GUI to look a certain way? (Java)

So, I'm brand spankin' new to programming, so thanks in advance for your help. I'm trying to put this base 2 to base 10/base 10 to base 2 calculator I have made into a GUI. For the life of me I can't figure out how to nicely format it. I'm trying to make it look like the following: The two radio buttons on top, the input textfield bellow those, the convert button bellow that, the output field bellow that, and the clear button bellow that. Any ideas on how I can accomplish this?
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
#SuppressWarnings("serial")
public class GUI extends JFrame implements ActionListener
{
private JTextField input;
private JTextField output;
private JRadioButton base2Button;
private JRadioButton base10Button;
private JButton convert;
private JButton clear;
private Container canvas = getContentPane();
private Color GRAY;
public GUI()
{
this.setTitle("Base 10-2 calc");
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//this.setLayout(new GridLayout(2,2));
base2Button = new JRadioButton( "Convert to base 2");
base10Button = new JRadioButton( "Convert to base 10");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(base2Button);
radioGroup.add(base10Button);
JPanel radioButtonsPanel = new JPanel();
radioButtonsPanel.setLayout( new FlowLayout(FlowLayout.LEFT) );
radioButtonsPanel.add(base2Button);
radioButtonsPanel.add(base10Button);
canvas.add(radioButtonsPanel);
base2Button.setSelected( true );
base10Button.setSelected( true );
input = new JTextField(18);
//input = new JFormattedTextField(20);
canvas.add(input);
output = new JTextField(18);
//output = new JFormattedTextField(20);
canvas.add(output);
convert = new JButton("Convert!");
convert.addActionListener(this);
canvas.add(convert);
clear = new JButton("Clear");
clear.addActionListener(this);
canvas.add(clear);
output.setBackground(GRAY);
output.setEditable(false);
this.setSize(300, 200);
this.setVisible(true);
this.setLocation(99, 101);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
GUI app = new GUI();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Convert!"))
{
String numS = input.getText();
int numI = Integer.parseInt(numS);
if(base2Button.isSelected())
{
output.setText(Integer.toBinaryString(Integer.valueOf(numI)));
}
if(base10Button.isSelected())
{
output.setText("" + Integer.valueOf(numS,2));
}
}
if(s.equals("Clear"))
{
input.setText("");
output.setText("");
}
}
}
For a simple layout, you could use a GridLayout with one column and then use a bunch of child panels with FlowLayout which align the components based on the available space in a single row. If you want more control, I'd suggest learning about the GridBagLayout manager which is a more flexible version of GridLayout.
public class ExampleGUI {
public ExampleGUI() {
init();
}
private void init() {
JFrame frame = new JFrame();
// Set the frame's layout to a GridLayout with one column
frame.setLayout(new GridLayout(0, 1));
frame.setPreferredSize(new Dimension(300, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Child panels, each with FlowLayout(), which aligns the components
// in a single row, until there's no more space
JPanel radioButtonPanel = new JPanel(new FlowLayout());
JRadioButton button1 = new JRadioButton("Option 1");
JRadioButton button2 = new JRadioButton("Option 2");
radioButtonPanel.add(button1);
radioButtonPanel.add(button2);
JPanel inputPanel = new JPanel(new FlowLayout());
JLabel inputLabel = new JLabel("Input: ");
JTextField textField1 = new JTextField(15);
inputPanel.add(inputLabel);
inputPanel.add(textField1);
JPanel convertPanel = new JPanel(new FlowLayout());
JButton convertButton = new JButton("Convert");
convertPanel.add(convertButton);
JPanel outputPanel = new JPanel(new FlowLayout());
JLabel outputLabel = new JLabel("Output: ");
JTextField textField2 = new JTextField(15);
outputPanel.add(outputLabel);
outputPanel.add(textField2);
// Add the child panels to the frame, in order, which all get placed
// in a single column
frame.add(radioButtonPanel);
frame.add(inputPanel);
frame.add(convertPanel);
frame.add(outputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ExampleGUI example = new ExampleGUI();
}
}
The end result:

Alignment of RadioButtons in Java GUI

Here is what my program is suppose to look like:
but I can't seem to get my radio buttons and my JLabel to be aligned properly. How do I align my radio buttons on the right and stacked? Also, how do I get my JLabel and JTextField to show stacked?
Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SeriesCalc extends JFrame {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private JFrame frame = new JFrame("Recursion");
private JPanel panel = new JPanel();
private JPanel labels = new JPanel();
private JPanel buttons = new JPanel();
private JPanel radioButtonsPanel = new JPanel();
private JLabel inputLabel = new JLabel("Enter i:");
private JLabel resultLabel = new JLabel("Result:");
private JTextField inputField = new JTextField(15);
private JTextField resultField = new JTextField(15);
private JButton compute = new JButton("Compute");
private JRadioButton iterative, recursive;
public SeriesCalc() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
radioButtonsPanel.add(iterative = new JRadioButton("Iterative"));
radioButtonsPanel.add(recursive = new JRadioButton("Recursive"));
add(radioButtonsPanel);
ButtonGroup radioButtons = new ButtonGroup();
radioButtons.add(iterative);
radioButtons.add(recursive);
iterative.addActionListener(new Calculations());
recursive.addActionListener(new Calculations());
compute.addActionListener(new Calculations());
resultField.setEditable(false);
panel.setLayout(new GridLayout(4, 1));
labels.add(inputLabel);
labels.add(inputField);
labels.add(resultLabel);
labels.add(resultField);
buttons.add(compute);
panel.add(radioButtonsPanel);
panel.add(labels);
panel.add(buttons);
frame.getContentPane().add(panel);
}
public void display() {
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
public class Calculations implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object calc = e.getSource();
try {
if (calc == compute) {
if (iterative.isSelected()) {
double n = Double.parseDouble(inputField.getText());
double product = 1;
for (int i = 3; i < n; i++) {
product *= i;
}
resultField.setText(Double.toString(product));
} else if (recursive.isSelected()) {
double i = Double.parseDouble(inputField.getText());
double y = 0;
if (i == 1) {
resultField.setText(Double.toString(i / (2. * i + 1)));
} else {
resultField.setText(Double.toString(i / (2. * i + 1)+ (i -1)));
}
}
}
} catch (NumberFormatException nfe) {
System.err.println(nfe.getMessage());
}
}
}
public static void main(String[] args) {
SeriesCalc calculator = new SeriesCalc();
calculator.display();
}
}
I see some errors in your program:
You're extending JFrame and creating an instance of it in the same program. Use one or the other (I recommend the latter), See Using extends vs calling it inside of class.
You're setting frame.setSize() while this isn't an error it's always better to call frame.pack() in your program. It will respect the minimum size where all the components are shown in their preferredSizes. If you need an exact size for your window override getPreferredSize() method instead.
You're not placing your program on the Event Dispatch Thread (EDT), this could cause threading issues in the future as Swing is not thread safe, this can be solved with:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Call your constructor here
}
});
}
but I can't seem to get my radio buttons and my JLabel to be aligned properly
This is because a JPanel's default layout manager is FlowLayout and thus it will place your components in a single row.
My idea to get to your desired GUI was to use a single GridLayout with 0 rows (it will add as many as needed) and 2 columns, and where you need a "blank" space you can add empty JLabels.
I didn't placed an ActionListener on my code as this question is about the GUI design not the logic inside it.
I think I'm not missing anything, this is the output that the below code creates:
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SeriesCalc {
private JFrame frame;
private JRadioButton iterative;
private JRadioButton recursive;
private ButtonGroup group;
private JLabel label;
private JLabel resultLabel;
private JTextField field;
private JTextField resultField;
private JButton computeButton;
private JPanel pane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new SeriesCalc().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new GridLayout(0, 2, 2, 5));
iterative = new JRadioButton("Iterative");
recursive = new JRadioButton("Recursive");
group = new ButtonGroup();
group.add(iterative);
group.add(recursive);
computeButton = new JButton("Compute");
label = new JLabel("Enter \"i\": ");
resultLabel = new JLabel("Result: ");
field = new JTextField(5);
resultField = new JTextField(5);
resultField.setEnabled(false);
pane.add(new JLabel(""));
pane.add(iterative);
pane.add(new JLabel(""));
pane.add(recursive);
pane.add(label);
pane.add(field);
pane.add(new JLabel(""));
pane.add(computeButton);
pane.add(resultLabel);
pane.add(resultField);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I donĀ“t know if you are using an IDE like Eclipse or NetBeans. In Eclipse for example, you can open this class with windowbuilder and you could visualize how your Frame will appear. Obviously you can move element in this view (window builder) and put all objects where you want and the size you want. In the next image you could see the view of an edit of a Frame if you open it with window builder in mode design in Eclipse. In this mode (design) you can put absolute layout to these objects and they can be put where you want with that layout. I recommend you to use an IDE like eclipse for Java Developing, it is very useful and it has a lot of facilities. I hope I could help you:
enter image description here

Button and textfield don't show up in Java

For school I had to make a JFrame and within that One button and Two textfields. Whatever you put in Textfield one have to get into textfield two when the button is pressed. I got the code to the point I should see the textfields and the button when i run the program. For whatever reason it doesn't.
My come so far:
package helloworld;
import javax.swing.*;
import java.awt.event.*;
public class HelloWorld extends JFrame {
public static void main(String[] args) {
JFrame frame = new HelloWorld();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Hello World Button App");
JPanel panel = new JPanel();
frame.setContentPane(panel);
fram.setVisible(true);
}
}
class panel extends JPanel {
public JButton btn1 = new JButton("Klick!");
public JTextField txt1 = new JTextField(10);
public JTextField txt2 = new JTextField(10);
public panel() {
add(btn1);
add(txt1);
add(txt2);
}
}
I am not yet allowed to post images but I will provide a link to the picture down here
I am sorry if this question allready exests but i couldnt's find a similar question.
I am new to programming so please dont yell at me when I forgot something or wrote something wrong in it!
Here i have modified your code a bit, but did in a similar way.
I won't extend JFrame until and unless i don't want to do something creative, but you always CAN.
You had already extended JFrame , so no worth of calling methods with frame.foo()
but simply foo() , and most important JFrame frame = new HelloWorld() , will make no sense, if you have already extended you class with JFrame:
import javax.swing.*;
public class HelloWorld extends JFrame{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new HelloWorld().setVisible(true);
}
});
}
public HelloWorld()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Hello World Button App");
panel pan= new panel();
add(pan.panel);
pack();
setVisible(true);
}
}
class panel {
private JButton btn1 = new JButton("Klick!");
private JTextField txt1 = new JTextField(10);
private JTextField txt2 = new JTextField(10);
JPanel panel;
public panel() {
panel = new JPanel();
panel.add(btn1);
panel.add(txt1);
panel.add(txt2);
}
}
Also, you can also extend your panel class with JPanel :
public HelloWorld()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Hello World Button App");
panel pan= new panel();
add(pan);
pack();
setVisible(true);
}
}
class panel extends JPanel {
private JButton btn1 = new JButton("Klick!");
private JTextField txt1 = new JTextField(10);
private JTextField txt2 = new JTextField(10);
public panel() {
add(btn1);
add(txt1);
add(txt2);
}
}
That is because your class name is panel not JPanel
Modify this:
panel panel = new panel();
frame.setContentPane(panel);
frame.setVisible(true);
You should try to use names for your Class that are not so confusing, and try to declare them with uppercase.
Example:
Class Panel extends JPanel {}
Object:
Panel panel = new Panel()
Here you can clearly read which one is the class name and which is the object (instance of that class) of that class.
You declared a class called panel that you are not using anywhere. Please replace the line bwlow:
JPanel panel = new JPanel();
with:
SomePanel panel = new SomePanel();
Then, your class panel becomes SomePanel to follow correct class naming.
Some thoughts to help you:
Name your classes following the Java style
Don't use public fields
Set layouts on your panels. This time it worked for you as the default is FlowLayout.

Unstable GUI in Java

I am writing a very simple GUI, that contains 3 buttons, 2 labels, 2 text fields and one text area. Strangely, the result is unstable: when running the class the GUI appears with random number of the controls. I tried various layout managers, changing the order among the control - nothing.
Can someone help?
package finaltestrunner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FinalTestGUI extends JFrame implements ActionListener
{
public Boolean startState = false;
JButton sofButton;
JButton startStopButton;
JButton exitButton;
JTextField loopCounts;
JTextField trSnField;
JTextArea resultField = null;
public FinalTestGUI()
{
// The constructor creates the panel and places the controls
super(); // Jframe constructor
JFrame trFrame = new JFrame();
trFrame.setSize(1000, 100);
trFrame.setVisible(true);
trFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
trFrame.setTitle("Test runner");
setFont(new Font("SansSerif", Font.PLAIN, 14));
// trFrame.setLayout(new FlowLayout());
JPanel trControlPanel = new JPanel();
trControlPanel.setSize(1000, 100);
trControlPanel.setLayout(new GridLayout(1,7));
exitButton = new JButton("Exit");
trControlPanel.add(exitButton);
startStopButton = new JButton("Run ");
trControlPanel.add(startStopButton);
JLabel loopsLabel = new JLabel ("Loops count: ");
trControlPanel.add(loopsLabel);
loopCounts = new JTextField (5);
trControlPanel.add(loopCounts);
sofButton = new JButton("SoF");
trControlPanel.add(sofButton);
JLabel testLabel = new JLabel ("serial Number: ");
trControlPanel.add(testLabel);
trSnField = new JTextField (5);
trControlPanel.add(trSnField);
JTextArea trResultField = new JTextArea (80, 10);
trFrame.add(trControlPanel);
// cpl.add(trResultField);
startStopButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed (ActionEvent trStartStopButton)
{
startState = !startState;
if (startState)
{
startStopButton.setText("Run ");
startStopButton.setForeground(Color.red);
}
else
{
startStopButton.setText("Stop");
startStopButton.setForeground(Color.green);
}
}
});
sofButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed (ActionEvent trSofButton)
{
loopCounts.setText("SOF\n");
}
});
exitButton.addActionListener (new ActionListener()
{
#Override
public void actionPerformed (ActionEvent trExitButton)
{
System.exit(0);
}
});
} // End of the constructor
#Override
public void actionPerformed (ActionEvent ae) { }
public void atpManager ()
{
String selectedAtp = "";
}
}
There are a couple of issues with this code:
You are already inheriting from JFrame, so you do not need to create yet another JFrame
You are showing your frame with setVisible(true) and afterwards adding components to it. This invalidates your layout, you need to revalidate afterwards (or move setVisible() to a position where you already added your components)
You are adding your components to the JFrame directly, but you need to use its contentpane. Starting with Java 1.5, the JFrame.add() methods automatically forward to the content pane. In earlier versions, it was necessary to retrieve the content pane with JFrame.getContentPane() to add the child components to the content pane.
Try this:
public FinalTestGUI() {
// The constructor creates the panel and places the controls
super(); // Jframe constructor
setSize(1000, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Test runner");
setFont(new Font("SansSerif", Font.PLAIN, 14));
setLayout(new FlowLayout());
JPanel trControlPanel = new JPanel();
trControlPanel.setSize(1000, 100);
trControlPanel.setLayout(new GridLayout(1,7));
exitButton = new JButton("Exit");
trControlPanel.add(exitButton);
startStopButton = new JButton("Run ");
trControlPanel.add(startStopButton);
JLabel loopsLabel = new JLabel ("Loops count: ");
trControlPanel.add(loopsLabel);
loopCounts = new JTextField (5);
trControlPanel.add(loopCounts);
sofButton = new JButton("SoF");
trControlPanel.add(sofButton);
JLabel testLabel = new JLabel ("serial Number: ");
trControlPanel.add(testLabel);
trSnField = new JTextField (5);
trControlPanel.add(trSnField);
JTextArea trResultField = new JTextArea (80, 10);
// getContentPane().add(trControlPanel); // pre 1.5
add(trControlPanel); // 1.5 and greater
setVisible(true);
}

Categories

Resources