error: illegal start of expression 1 error - java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.lang.*;
import java.util.*;
public class Life {
public static void main (String[] args){
JFrame frame = new JFrame("Interest Calculator");
frame.setVisible(true);
frame.setSize(300,100);
frame.setBackground(Color.magenta);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Simple Interest");
panel.add(button);
button.addActionListener (new Action1());
JButton button2 = new JButton("Compound Interest");
panel.add(button2);
button2.addActionListener (new Action2());
}
static class Action1 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Simple");
frame2.setVisible(true);
frame2.setSize(300,100);
JPanel panel = new JPanel();
frame2.add(panel);
JButton button = new JButton("Ordinary");
panel.add(button);
button.addActionListener (new Ordinary());
JButton button2 = new JButton("Exact");
panel.add(button2);
button2.addActionListener (new Exact());
}
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame3 = new JFrame("Compound Interest");
frame3.setVisible(true);
frame3.setSize(300,100);
JPanel panel = new JPanel();
frame3.add(panel);
JButton button = new JButton("Compounded");
panel.add(button);
JButton button2 = new JButton("Continously");
panel.add(button2);
}
}
static class Ordinary implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame4 = new JFrame("Simple");
frame4.setVisible(true);
frame4.setSize(300,100);
JPanel panel = new JPanel();
frame4.add(panel);
JButton button = new JButton("Approximate");
panel.add(button);
button.addActionListener (new Approximate());
JButton button2 = new JButton("Actual");
panel.add(button2);
}
}
static class Exact implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame5 = new JFrame("Exact");
frame5.setVisible(true);
frame5.setSize(300,100);
JPanel panel = new JPanel();
frame5.add(panel);
JButton button = new JButton("Approximate");
panel.add(button);
JButton button2 = new JButton("Actual");
panel.add(button2);
}
}
static class Approximate implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame6 = new JFrame("Simple");
frame6.setVisible(true);
frame6.setSize(300,100);
JPanel panel = new JPanel();
frame6.add(panel);
frame6.setVisible(true);
frame6.setSize(300,300);
frame6.setLayout(null);
frame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField P = new JTextField();
panel.add(P);
P.setText("Enter Principal");
JTextField r = new JTextField();
panel.add(r);
r.setText("Enter Rate");
JTextField t = new JTextField();
panel.add(t);
t.setText("Enter Year");
JButton button = new JButton("Calculate");
panel.add(button);
button.addActionListener (new button());
static class button implements ActionListener {
public void actionPerformed (ActionEvent e) {
float P,r,t,result ;
P = Float.parseFloat(P.getText());
r = Float.parseFloat(r.getText());
t = Float.parseFloat(t.getText());
result = P*r/100*t/360;
button.setText(String.valueof(result));
}
}
}
}
}
So this is my code to create a compound calculator but I have a problem it is the illegal start of expression.please help me for my project.

Below is partially fixed part of the code which causes problems. Next time include full error code, and try to show only relevant part of the code.
ActionListener listener = new ActionListener() {
public void actionPerformed (ActionEvent e) {
float P,r,t,result ;
//P = Float.parseFloat(P.getText()); // P makes no sens here - its float !!! and uninitialized
//r = Float.parseFloat(r.getText()); // same for r
//t = Float.parseFloat(t.getText()); // same for t
result = P*r/100*t/360;
button.setText(String.valueOf(result));
}
};
button.addActionListener (listener);

Related

Gui Freeze after pressing a button

I have a problem that my whole gui freezes when I try to press button level 1. my main problem is to set a new panel which is empty one for now.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Guibach extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel mainPanel;
private JPanel choosingPanel;
private JPanel x;
private JButton exit;
private JButton but1;
private JButton but2;
private JButton but3;
private JButton but4;
private JButton but5;
private JButton but6;
private JButton but7;
private JButton but8;
private JButton but9;
private JButton but10;
public Guibach() {
this.setSize(900, 900);
this.setLayout(null);
mainPanel = new JPanel();
mainPanel.setSize(900, 900);
mainPanel.setBackground(Color.GRAY);
mainPanel.setLayout(null);
x = new JPanel();
x.setSize(900, 900);
x.setBackground(Color.RED);
x.setLayout(new BorderLayout());
choosingPanel = new JPanel();
choosingPanel.setSize(900, 450);
choosingPanel.setBackground(Color.BLUE);
choosingPanel.setLayout(new FlowLayout());
but1 = new JButton();
but1.setText("Level 1");
but1.setSize(10, 5);
but1.setActionCommand("but1");
but1.addActionListener(this);
choosingPanel.add(but1);
but2 = new JButton();
but2.setText("Level 2");
but2.setActionCommand("but2");
but2.addActionListener(this);
choosingPanel.add(but2);
but3 = new JButton();
but3.setText("Level 3");
but3.setActionCommand("but3");
but3.addActionListener(this);
choosingPanel.add(but3);
but4 = new JButton();
but4.setText("Level 4");
but4.setActionCommand("but4");
but4.addActionListener(this);
choosingPanel.add(but4);
but5 = new JButton();
but5.setText("Level 5");
but5.setActionCommand("but5");
but5.addActionListener(this);
choosingPanel.add(but5);
but6 = new JButton();
but6.setText("Level 6");
but6.setActionCommand("but6");
but6.addActionListener(this);
choosingPanel.add(but6);
but7 = new JButton();
but7.setText("Level 7");
but7.setActionCommand("but7");
but7.addActionListener(this);
choosingPanel.add(but7);
but8 = new JButton();
but8.setText("Level 8");
but8.setActionCommand("but8");
but8.addActionListener(this);
choosingPanel.add(but8);
but9 = new JButton();
but9.setText("Level 9");
but9.setActionCommand("but9");
but9.addActionListener(this);
choosingPanel.add(but9);
but10 = new JButton();
but10.setText("Level 10");
but10.setActionCommand("but10");
but10.addActionListener(this);
choosingPanel.add(but10);
exit = new JButton();
exit.setText("Exit");
exit.setActionCommand("exit");
exit.addActionListener(this);
choosingPanel.add(exit);
mainPanel.add(choosingPanel);
this.add(mainPanel);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("but1")) {
choosingPanel.setVisible(false);
mainPanel.removeAll();
getContentPane().removeAll();
getContentPane().add(x);
x.setVisible(true);
revalidate();
}
if (e.getActionCommand().equals("but2")) {
}
if (e.getActionCommand().equals("but3")) {
}
if (e.getActionCommand().equals("but4")) {
}
if (e.getActionCommand().equals("but5")) {
}
if (e.getActionCommand().equals("but6")) {
}
if (e.getActionCommand().equals("but7")) {
}
if (e.getActionCommand().equals("but8")) {
}
if (e.getActionCommand().equals("but9")) {
}
if (e.getActionCommand().equals("but10")) {
}
if (e.getActionCommand().equals("exit")) {
WindowDestroyer myListener = new WindowDestroyer();
this.addWindowListener(myListener);
System.exit(0);
}
}
public static void main(String[] args) {
new Guibach();
}
}
I need to clear all the things and place a new empty panel which is X in my code.

Why launching does it random?

When i run this program sometimes shows me all buttons, but sometimes only 2 or 3 or 4 or 5 or even just 1.. why is that??
I really do not get it. There should always be 6 buttons, but it doesnt show them. Is there any logical reason?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class testnet
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Knjigarna");
frame.setVisible(true);
frame.setSize(800,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JButton button1 = new JButton("Prikazi vse");
panel.add(button1);
button1.addActionListener (new Action1());
JButton button2 = new JButton("Prikazi knjigo");
panel.add(button2);
button2.addActionListener (new Action2());
JButton button3 = new JButton("Dodaj knjigo");
panel.add(button3);
button3.addActionListener (new Action3());
JButton button4 = new JButton("Brisi knjigo");
panel.add(button4);
button4.addActionListener (new Action4());
JButton button5 = new JButton("Uredi knjigo");
panel.add(button5);
button5.addActionListener (new Action5());
JButton button6 = new JButton("Izhod");
panel.add(button6);
button6.addActionListener (new Action6());
}
static class Action1 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame2 = new JFrame("Pikaz vseh knjig");
frame2.setVisible(true);
frame2.setSize(500,800);
JLabel label = new JLabel("Seznam vseh knjig:");
JPanel panel = new JPanel();
JTextField text1=new JTextField("Naslov: ");
JTextField text2=new JTextField("Avtor: ");
frame2.add(panel);
panel.add(label);
panel.add(text1);
panel.add(text2);
}
}
static class Action2 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame3 = new JFrame("Prikaz knjige");
frame3.setVisible(true);
frame3.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige:");
JPanel panel = new JPanel();
frame3.add(panel);
panel.add(label);
}
}
static class Action3 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame4 = new JFrame("Dodajanje knjige");
frame4.setVisible(true);
frame4.setSize(600,300);
JLabel label = new JLabel("Vpisi podtke o knjigi");
JPanel panel = new JPanel();
frame4.add(panel);
panel.add(label);
}
}
static class Action4 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame5 = new JFrame("Brisanje knjige");
frame5.setVisible(true);
frame5.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige, ki jo zelis brisati");
JPanel panel = new JPanel();
frame5.add(panel);
panel.add(label);
}
}
static class Action5 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
JFrame frame6 = new JFrame("Urejanje knjige");
frame6.setVisible(true);
frame6.setSize(600,300);
JLabel label = new JLabel("Vpisi naslov knjige, ki jo zelis urejati");
JPanel panel = new JPanel();
frame6.add(panel);
panel.add(label);
}
}
static class Action6 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
}
try something with layout. JFrame and or remove managed by inside with a content pane. content pane default layout is BorderLayout. so you need to try border layout stuff.
or you can try this code in you main method
frame.setLayout(new FlowLayout());
this will add component by one by one.
for more about layout you can get in here
This is just a fix and not really an explanation of why the problem is occurring.
Call frame.revalidate() after adding all the buttons.
From the Java Docs,
public Component add(Component comp)
This method
changes layout-related information, and therefore, invalidates the
component hierarchy. If the container has already been displayed, the
hierarchy must be validated thereafter in order to display the added
component.

How am I to run my GUI? Or to be more exact, what do I put in my main?

public class StatsGUI extends JFrame implements ActionListener {
JLabel label;
JLabel label2;
JTextField input;
JTextField output;
JButton getButton;
JButton exitButton;
public StatsGUI()
{
JPanel panel = new JPanel();
label = new JLabel("Enter number");
panel.add(label);
input = new JTextField(10);
input.addActionListener(this);
panel.add(input);
label2 = new JLabel("Statistics");
output = new JTextField(10);
output.setEditable(false);
panel.add(output);
getButton = new JButton("Go");
getButton.addActionListener(this);
panel.add(getButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
panel.add(exitButton);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exitButton)
{
System.exit(0);
}
else
{
String text = input.getText();
output.setText(text + "COUNTER");
}
}
public static void main(String[] args)
{
}
This is my simple GUI program. I have placed all buttons and other gadgets within the constructor. However, I am not sure what I should be putting inside my main in order to get my GUI to actually show up. I am sure I am missing something incredibly simple here however I am not sure what. Help would be much appreciated.
You are not that far from getting things to work. Just a few things to know:
Your UI should be started on the Event dispatching thread (EDT)
You actually need to add your panels/components to your frame
You need to pack() your Window/Frame
You need to make it visible
(Design stuff, optional but when you are at it, why not just fix that as well), no need to extend JFrame, so let's just drop that.
So eventually, taking these advices into consideration leads you to something like this:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class StatsGUI implements ActionListener {
JLabel label;
JLabel label2;
JTextField input;
JTextField output;
JButton getButton;
JButton exitButton;
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exitButton) {
System.exit(0);
} else {
String text = input.getText();
output.setText(text + "COUNTER");
}
}
public void initUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
label = new JLabel("Enter number");
panel.add(label);
input = new JTextField(10);
input.addActionListener(this);
panel.add(input);
label2 = new JLabel("Statistics");
output = new JTextField(10);
output.setEditable(false);
panel.add(output);
getButton = new JButton("Go");
getButton.addActionListener(this);
panel.add(getButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
panel.add(exitButton);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new StatsGUI().initUI();
}
});
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class StatsGUI extends JFrame implements ActionListener {
JLabel label;
JLabel label2;
JTextField input;
JTextField output;
JButton getButton;
JButton exitButton;
public StatsGUI()
{
JPanel panel = new JPanel();
label = new JLabel("Enter number");
panel.add(label);
input = new JTextField(10);
input.addActionListener(this);
panel.add(input);
label2 = new JLabel("Statistics");
output = new JTextField(10);
output.setEditable(false);
panel.add(output);
getButton = new JButton("Go");
getButton.addActionListener(this);
panel.add(getButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
panel.add(exitButton);
add(panel);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exitButton)
{
System.exit(0);
}
else
{
String text = input.getText();
output.setText(text + "COUNTER");
}
}
public static void main(String[] args)
{
StatsGUI s= new StatsGUI();
s.setVisible(true);
s.setSize(1000,1000);
}
}
You are not calling a contructor .call costructor like 'StatsGUI s= new StatsGUI();' or 'new StatsGUI();' .

Generating non-repeating random methods in GUI

I've been spending so long looking at my computer monitor because I really don't know what to do to prevent the frames on my program on appearing simultaneously when I click the Start button.
Here's my main class:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;
public class PopQuizDemo {
public static void main (String args[]){
PopQuizDemo();
}
public static void PopQuizDemo(){
final SimpleFrame frame = new SimpleFrame();
final JPanel main = new JPanel();
main.setSize(400,75);
main.setLayout(new GridLayout(3,1));
frame.add(main);
JLabel l1 = new JLabel("Welcome to POP Quiz!");
main.add(l1);
JLabel l2 = new JLabel("Enter your name:");
main.add(l2);
final JTextField name = new JTextField ();
main.add(name);
final JPanel panel = new JPanel();
panel.setSize(400,50);
panel.setLocation(0,225);
frame.add(panel);
JButton start = new JButton ("Start");
panel.add(start);
frame.setVisible(true);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
randomize();
}
});
}
public static void randomize(){
Questions q = new Questions();
int a=0;
Random randnum = new Random (System.currentTimeMillis());
java.util.HashSet<Integer> myset = new java.util.HashSet<>();
for (int count = 1; count <= 3; count++){
while (true) {
a = randnum.nextInt (3);
if(!myset.contains(a)) { myset.add(new Integer(a)); break;}
}
if(a==0){
q.one();
}
else if(a==1){
q.two();
}
else if(a==2){
q.three();
}
else{
break;
}
}
}
}
And here is the class Question where I get the methods one(), two(), and three():
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Questions {
public static void one(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("In computers, what is the smallest and basic unit of");
JLabel qu2 = new JLabel();
qu2.setText("information storage?");
p1.add(qu1);
p1.add(qu2);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Bit");
p2.add(a);
JButton b = new JButton("b. Byte");
p2.add(b);
JButton c = new JButton("c. Data");
p2.add(c);
JButton d = new JButton("d. Newton");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
public static void two(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("Machine language is also known as __________.");
p1.add(qu1);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Low level language");
p2.add(a);
JButton b = new JButton("b. Assembly language");
p2.add(b);
JButton c = new JButton("c. High level language");
p2.add(c);
JButton d = new JButton("d. Source code");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
public static void three(){
final SimpleFrame frame = new SimpleFrame();
JPanel p1 = new JPanel();
p1.setSize(400,100);
frame.add(p1);
JLabel qu1 = new JLabel();
qu1.setText("What is the shortcut key of printing a document for");
JLabel qu2 = new JLabel();
qu2.setText("computers using Windows?");
p1.add(qu1);
p1.add(qu2);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
frame.add(p2);
JButton a = new JButton("a. Ctrl + P");
p2.add(a);
JButton b = new JButton("b. Shift + P");
p2.add(b);
JButton c = new JButton("c. Shift + PP");
p2.add(c);
JButton d = new JButton("d. Alt + P");
p2.add(d);
frame.setVisible(true);
final PopQuizDemo demo = new PopQuizDemo();
a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
d.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setVisible(false);
demo.randomize();
}
});
}//end of method
}
The only problem here is that the when I call the method randomize() in the action listener in the Start button, it shows all the frames. Yes, they are not repeating but it shows simultaneously. I don't know where the problem is. Is it with the method randomize, the looping, the questions? Can someone help me? Please? Big thanks.
PS:
This is the class SimpleFrame
import javax.swing.JFrame;
public class SimpleFrame extends JFrame{
public SimpleFrame(){
setSize(400,300);
setTitle("Pop Quiz!");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setResizable(false);
}
}
You can use model dialog to show one after another using a for loop as show below:
public static void main(String[] args) {
JFrame m = new JFrame("Hello");
m.setSize(200,200);
m.setVisible(true);
for(int i=0;i<3;i++) {
JDialog dlg = new JDialog(m,"Dialog",true);
dlg.setSize(100,100);
dlg.show();
}
}

JButton Layout Issue

I'm putting together the basic layout for a contacts book, and I want to know how I can make the 3 test buttons span from edge to edge just as the arrow buttons do.
private static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Code Placeholder");
}
}
public static void main(String[] args) {
//down button
ImageIcon downArrow = new ImageIcon("down.png");
JButton downButton = new JButton(downArrow);
ButtonHandler downListener = new ButtonHandler();
downButton.addActionListener(downListener);
//up button
ImageIcon upArrow = new ImageIcon("up.png");
JButton upButton = new JButton(upArrow);
ButtonHandler upListener = new ButtonHandler();
upButton.addActionListener(upListener);
//contacts
JButton test1Button = new JButton("Code Placeholder");
JButton test2Button = new JButton("Code Placeholder");
JButton test3Button = new JButton("Code Placeholder");
Box box = Box.createVerticalBox();
box.add(test1Button);
box.add(test2Button);
box.add(test3Button);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(box, BorderLayout.CENTER);
content.add(downButton, BorderLayout.SOUTH);
content.add(upButton, BorderLayout.NORTH);
JFrame window = new JFrame("Contacts");
window.setContentPane(content);
window.setSize(400, 600);
window.setLocation(100, 100);
window.setVisible(true);
}
Following up on #kloffy's suggestion:
package playground.tests;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import junit.framework.TestCase;
public class ButtonTest extends TestCase {
public void testThreeButtons() throws Exception {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout());
JButton button1 = new JButton("A");
JButton button2 = new JButton("B");
JButton button3 = new JButton("C");
panel.add(button1);
panel.add(button2);
panel.add(button3);
JFrame window = new JFrame("Contacts");
window.setContentPane(panel);
window.setSize(300, 600);
window.pack();
window.setVisible(true);
int width = button1.getWidth();
assertEquals(width, button2.getWidth());
assertEquals(width, button3.getWidth());
}
}

Categories