How to create custom panel layout with Swing? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class BattleShipsMain {
public static void main(String[] args) {
// JButton arrays to hold buttons
JButton[] userButtons = new JButton[100];
JButton[] compButtons = new JButton[100];
// Text for ships label
String shipsText = "Ships Size (Squares)" + "Carrier 5"
+ "Battleship 4" + "Destroyer 3"
+ "Patrol Boat 2";
// Draw main window and set layout
JFrame window = new JFrame("Battle Ships");
window.setSize(1200, 1900);
window.getContentPane().setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Draw top game panel
JPanel gridPanTop = new JPanel();
gridPanTop.setLayout(new BorderLayout());
gridPanTop.setPreferredSize(new Dimension(1300, 400));
gridPanTop.setBackground(Color.GRAY);
// Top panel text
JLabel ships = new JLabel();
ships.setText(shipsText);
// Bottom panel buttons
JButton submit = new JButton("Submit");
Dimension submitSize = new Dimension(20, 20);
submit.setSize(submitSize);
// Draw bottom game panel
JPanel panBottom = new JPanel();
panBottom.setBackground(Color.WHITE);
panBottom.setLayout(new BorderLayout());
panBottom.setPreferredSize(new Dimension(200, 200));
panBottom.add(submit);
// Set position of game panels
window.getContentPane().add(gridPanTop, BorderLayout.PAGE_START);
window.getContentPane().add(panBottom, BorderLayout.CENTER);
// Set border for grid buttons
Border border = new LineBorder(Color.gray);
// Draw panel for grids
JPanel user = new JPanel();
JPanel comp = new JPanel();
user.setBackground(Color.gray);
comp.setBackground(Color.gray);
user.setBorder(border);
comp.setBorder(border);
// Set layout for grid panels
user.setLayout(new GridLayout(10, 10));
comp.setLayout(new GridLayout(10, 10));
int x = userButtons.length;
// Set user buttons as JButtons, set size and add to grid
for (int i = 0; i < x; i++) {
userButtons[i] = new JButton();
userButtons[i].setPreferredSize(new Dimension(40, 40));
user.add(userButtons[i]);
}
// Set computer buttons as JButtons, set size and add to grid
for (int i = 0; i < x; i++) {
compButtons[i] = new JButton();
compButtons[i].setPreferredSize(new Dimension(40, 40));
comp.add(compButtons[i]);
}
// Add panels to main frame and set visible
window.pack();
window.add(gridPanTop);
window.add(panBottom);
gridPanTop.add(user, BorderLayout.WEST);
gridPanTop.add(comp, BorderLayout.EAST);
gridPanTop.setVisible(true);
panBottom.setVisible(true);
window.setVisible(true);
user.setVisible(true);
comp.setVisible(true);
// Start main game
MainGame start = new MainGame();
}
}
I have an assignment and am having lot of trouble creating the below panel layout in Java Swing. I have had no luck using any of the layouts.
Could anyone help my with this layout?
At present the code displays the following output:
You can probably tell I am a beginner so please excuse rookie errors. The panel layout I have at the moment LOOKS like the ideal one I attached but clearly is not the correct layout as I'd like.

You can achive this with different layouts and compound of layouts (using subpanels). I would have used GridBagLayout, that is definitely one of the most versatile layouts.
Example code
public class TestLayout extends JFrame {
private static final long serialVersionUID = -7619921429181915663L;
public TestLayout(){
super("TestLayout");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
private void init() {
this.getContentPane().setLayout(new GridBagLayout());
//Setup some test panel with labels
JPanel panel1 = new JPanel(new BorderLayout());
panel1.setBorder(BorderFactory.createEtchedBorder());
JLabel label1 = new JLabel("Panel 1");
label1.setHorizontalAlignment(SwingConstants.CENTER);
panel1.add(label1,BorderLayout.CENTER);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.setBorder(BorderFactory.createEtchedBorder());
JLabel label2 = new JLabel("Panel 2");
label2.setHorizontalAlignment(SwingConstants.CENTER);
panel2.add(label2,BorderLayout.CENTER);
JPanel panel3 = new JPanel(new BorderLayout());
panel3.setBorder(BorderFactory.createEtchedBorder());
JLabel label3 = new JLabel("Panel 3");
label3.setHorizontalAlignment(SwingConstants.CENTER);
panel3.add(label3,BorderLayout.CENTER);
JPanel panel4 = new JPanel(new BorderLayout());
panel4.setBorder(BorderFactory.createEtchedBorder());
JLabel label4 = new JLabel("Panel 4");
label4.setHorizontalAlignment(SwingConstants.CENTER);
panel4.add(label4,BorderLayout.CENTER);
//Here goes the interesting code
this.getContentPane().add(panel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.6, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2,
2, 2), 0, 0));
this.getContentPane().add(panel2, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.6, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2,
2, 2), 0, 0));
this.getContentPane().add(panel3, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.6, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2,
2, 2), 0, 0));
//next row
this.getContentPane().add(panel4, new GridBagConstraints(0, 1, 3, 1, 1.0, 0.4, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2,
2, 2), 0, 0));
this.setPreferredSize(new Dimension(400, 200));
this.pack();
}
public static void main(String[] args) {
TestLayout frame = new TestLayout();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Generated output
The key part of the GridBagLayout is the GridBagConstraints
new GridBagConstraints(columnNumber, rowNumber, columnSpan, rowSpan, columnWeigth, rowWeigth, alignment, fillType, insets, padX, pady)
see in example how rowWeigth is set to 0.6 for first row and 0.4 to second row so that first row takes up some more space (60%). Adjust them as you like (if you like same space, just set 0.5 to both).

Related

Why is the window empty when I run my java program?

I am trying to create a GUI in java. I am adding buttons, labels, text fields, and combo boxes. When I run my program, the window that pops up is completely blank. Any ideas why this is happening?
public Project3() {
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(7, 2));
leftPanel.setBounds(20, 10, 5, 5);
leftPanel.add(shapeLbl);
leftPanel.add(shapeBox);
leftPanel.add(fillLbl);
leftPanel.add(fillBox);
leftPanel.add(colorLbl);
leftPanel.add(colorBox);
leftPanel.add(widthLbl);
leftPanel.add(widthTxt);
leftPanel.add(heightLbl);
leftPanel.add(heightTxt);
leftPanel.add(xLbl);
leftPanel.add(xTxt);
leftPanel.add(yLbl);
leftPanel.add(yTxt);
add(leftPanel);
JPanel rightPanel = new JPanel();
rightPanel.setBounds(270, 10, 5, 5);
rightPanel.setBorder(BorderFactory.createTitledBorder("Shape Drawing"));
add(rightPanel);
JPanel botPanel = new JPanel();
botPanel.setBounds(240, 250, 5, 5);
botPanel.add(drawBtn);
add(botPanel);
drawBtn.setToolTipText("Using the information above, this button " +
"draws a shape in the area titled \"Shape Drawing.\"");
drawBtn.setMnemonic('d');
}
public static void main(String[] args) {
Project3 p3 = new Project3();
p3.setTitle("Geometric Drawing");
p3.setLayout(null);
p3.setSize(500, 301);
p3.setLocationRelativeTo(null);
p3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p3.setVisible(true);
}
}

Changing width of GridLayout elements

I want to change each buttons width but it's not working (i'am new in swing)
Panel = new JPanel();
gl = new GridLayout(2, 1);
Panel.setLayout(gl);
add(Panel, BorderLayout.EAST);
Up= new JButton("Btn1");
Up.addActionListener(this);
Up.setPreferredSize(new Dimension(200,150));
Panel.add(Up);
Down = new JButton("Btn2");
Down.addActionListener(this);
Down.setPreferredSize(new Dimension(200,300));
Panel.add(Down);
For this case I would use the GridBagLayout. This can be adapted very well with the GridBagConstraints.
In your case the weighty parameter would be responsible for controlling the height of the button in the panel. This is given as a percentage.
The code would look like this:
panel = new JPanel(new GridBagLayout());
add(panel, BorderLayout.EAST);
up = new JButton("Btn1");
up.addActionListener(this);
up.setPreferredSize(new Dimension(200, 150));
panel.add(up, new GridBagConstraints(0, 0, 1, 1, 100, 33, GridBagConstraints.NORTH, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 0,0));
down = new JButton("Btn2");
down.addActionListener(this);
down.setPreferredSize(new Dimension(200, 300));
panel.add(down, new GridBagConstraints(0, 1, 1, 1, 100, 66, GridBagConstraints.NORTH, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 0,0));
The result looks like this:
This question already has an accepted answer, but just wanted to put some 'tweaks' into an answer.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class BigWideButtons {
private JComponent ui = null;
static int upCodepoint = 9206; // upward triangle
static int downCodepoint = 9207; // downward triangle
Insets insets = new Insets(0, 75, 0, 75); // wide insets
Font font = getCompatibleFont().deriveFont(120f); // big font
BigWideButtons() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridLayout(0, 1, 10, 10));
ui.setBorder(new EmptyBorder(4,4,4,4));
addButton(upCodepoint);
addButton(downCodepoint);
}
private void addButton(int codepoint) {
JButton b = new JButton(new String(Character.toChars(codepoint)));
b.setFont(font);
b.setMargin(insets);
ui.add(b);
}
private static Font getCompatibleFont() {
Font[] fonts = GraphicsEnvironment.
getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
if (font.canDisplay(upCodepoint) &&
font.canDisplay(downCodepoint)) {
System.out.println("Font: " + font.getFamily());
return font;
}
}
return null; // No installed font supports these characters!
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
BigWideButtons o = new BigWideButtons();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}
This answer:
Uses a GridLayout, as seen in the question.
Avoids the need to guess a required size - it simply makes the font and button margin large, then packs the frame to fit them.
Given the meaning of the buttons 'up & down' it uses triangles pointing in those directions, as available in the Unicode character set.
But fonts do not typically have glyphs for every Unicode character (the character set is huge), so it adds a method which finds a font (the first available) which supports those glyphs. Here it uses "Segoe UI Symbol".

Java swing resize ImageIcon for different Windows Scaling settings

I am trying to create a small swing application using Zulu JDK 11. In the bottom right part of it I am having an image (logo). My problem is that the logo is displayed poorly when the user changes the scaling to a value greater than 100% (125%, 150%, 175% etc.)
Here is my source code.
Main class:
public class Main {
public static void main(String[] args) {
FrameServer frameServer = new FrameServer();
frameServer.setVisible(true);
}}
FrameServer class:
public class FrameServer extends JFrame {
private JPanel contentPane;
private JPanel centerPane = new JPanel();
private JPanel southPane = new JPanel();
private JPanel rightPane = new JPanel();
private JButton btnOK = new JButton("OK");
private JTabbedPane jTabbedPane1 = new JTabbedPane();
private Tab1 tab1Page = new Tab1();
private Tab2 tab2Page = new Tab2();
public FrameServer() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
jbInit();
pack();
centerFrame();
this.repaint();
}
private void jbInit() {
setTitle("Test Scaling App");
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BorderLayout());
centerPane.setLayout(new BorderLayout());
centerPane.add(jTabbedPane1, BorderLayout.CENTER);
southPane.setLayout(new BorderLayout());
rightPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
rightPane.add(btnOK);
CustomStatusBar statusBar = new CustomStatusBar();
southPane.add(statusBar, BorderLayout.CENTER);
southPane.add(rightPane, BorderLayout.EAST);
jTabbedPane1.addTab("Tab 1", tab1Page);
jTabbedPane1.addTab("Tab 2", tab2Page);
contentPane.add(centerPane, BorderLayout.CENTER);
contentPane.add(southPane, BorderLayout.SOUTH);
}
private void centerFrame() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
class Tab2 extends JScrollPane {
public Tab2() {
super(new JTextArea(10, 60));
super.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
super.getViewport().setPreferredSize(new Dimension(500, 300));
}
}
class Tab1 extends JPanel {
public Tab1() {
contentPane = this;
JScrollPane centerScrollPane = new JScrollPane(new JTable());
centerScrollPane.getViewport().setBackground(Color.white);
JPanel eastPane = new JPanel();
eastPane.setLayout(new FlowLayout());
JPanel topPane = new JPanel();
topPane.setLayout(new BoxLayout(topPane, BoxLayout.Y_AXIS));
eastPane.add(topPane, FlowLayout.LEFT);
contentPane.setLayout(new BorderLayout());
contentPane.add(centerScrollPane, BorderLayout.CENTER);
contentPane.add(eastPane, BorderLayout.EAST);
}
}
class CustomStatusBar extends JPanel {
JLabel label0 = new JLabel("", SwingConstants.CENTER);
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel("", SwingConstants.CENTER);
JLabel labelLogo = new JLabel();
private static final int HEIGHT = 21;
public CustomStatusBar() {
setCommonFeatures(label0, 40, "Stuff 0");
setCommonFeatures(label1, 120, "Stuff 1");
setCommonFeatures(label2, 120, "Stuff 2");
setCommonFeatures(label3, 90, "Stuff 3");
labelLogo.setFont(new java.awt.Font("Dialog", 3, 12));
labelLogo.setForeground(Color.black);
labelLogo.setBorder(BorderFactory.createLoweredBevelBorder());
ImageIcon image = new ImageIcon("small.png");
labelLogo.setIcon(image);
labelLogo.setPreferredSize(new Dimension(40, HEIGHT));
labelLogo.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(new GridBagLayout());
setMaximumSize(new Dimension(800, 40));
add(label0, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
add(label1, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
add(label2, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
add(label3, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
add(labelLogo, new GridBagConstraints(4, 0, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 50, 0));
}
private void setCommonFeatures(JLabel label, int width, String msg) {
label.setBackground(UIManager.getColor("ScrollBar.track"));
label.setForeground(Color.black);
label.setBorder(BorderFactory.createLoweredBevelBorder());
label.setPreferredSize(new Dimension(width, HEIGHT));
label.setText(" " + msg);
}
}}
Small.png image file:
When opened at scaling 100%, it looks good:
But when changing the scaling to 175%:
the pixels start to show up (open link to the picture to see better)
Now, what I have done is to replace the logo with one with a higher resolution:
Now, I am struck as I do not know how to make it fit in the space available for it and display properly when scaled. If i redimension the image from the java code as seen here, it will display properly for 100%, but for 175% scaling the pixels become visible again, like using the small.png file.
Which is the correct way of solving this?
Thank you.
The workaround is the following (only tested on Windows 10):
Locate the javaw.exe that the application is using (it very important to apply the following to javaw.exe, not java.exe)
Right click -> Properties -> Compatibility tab
Click button Change high DPI settings
Check Override high DPI scaling behavior. Scaling performed by: Select System in the combobox.
Press OK twice and restart the application

Resizing and icon on a jpanel with card layout

I'm trying to create an application where a list of numbers are listed on the WEST region of BorderLayout and corresponding panels in the CENTER. The problem is that
I need the WEST region to be wider. Right now, it contains JList in a JPanel which resizes it to its default size. Preferred size? Not sure!
I need to have an icon or alphabet character 'x' towards the right end of 1 or 2. So, that when I close it, I can close the corresponding panel on the CENTER.
CODE:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import java.awt.Color;
public class Sample1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample1 frame = new Sample1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
contentPane.add(panel, BorderLayout.WEST);
Vector<String> str = new Vector<>();
str.add("1");
str.add("2");
final JList list = new JList();
list.setListData(str);
panel.add(list);
final JPanel panel_1 = new JPanel();
panel_1.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new CardLayout(0, 0));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2, "1");
JLabel lblNewLabel = new JLabel("First");
panel_2.add(lblNewLabel);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3, "2");
JLabel lblNewLabel_1 = new JLabel("Second");
panel_3.add(lblNewLabel_1);
list.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e){
CardLayout layout = (CardLayout) panel_1.getLayout();
layout.show(panel_1, list.getSelectedValue().toString());
}
});
}
}
Maybe use a JTable with two columns. The second column can contain a close button. See Table Button Column for an example of this approach.
Otherwise you would need to add a panel to the WEST. The panel would contain the JList and some other component beside the JList to act as the close button. The problem with this approach is getting the components to line up and for the button to know which panel to close.
Have you tried using a GridLayout or BoxLayout also try setting up your GUI with a separate methods.
Setup GUI Code:
private void setupGUI() {
JPanel westP = setupWestPane();
getContentPane().add(westPa, BorderLayout.WEST);
}
Setup West Pane Code:
private JPanel setupWestPane() {
JPanel westP = setupWestPanel();
JPanel p = new JPanel(new GridLayout(1, 2, 20, 0));
p.setOpaque(false);
p.add(westP);
return p;
}
Setup West Panel Code :
private JPanel setupPlayerPanel() {
list1 = new JList();
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// If you want to add a listener to the listbox
// list1.addListSelectionListener(this);
JScrollPane jsp = new JScrollPane(playerList);
JPanel p = new JPanel(new BorderLayout());
p.setOpaque(false);
p.add(new JLabel("List"), BorderLayout.NORTH);
p.add(jsp, BorderLayout.CENTER);
return p;
}
The code above is just an example.

Panels overlap each other when box not big enough

I have this gui; and when the height is not big enough the panes will overlap each other. I have to set it at least 200, so I can completely see the two rows; but when it is set at 200, then I have like a big empty row at the end, and I don't want that. How could I fix this? Thanks.
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JButton panicButton;
JButton dontPanic;
JButton blameButton;
JButton newsButton;
JButton mediaButton;
JButton saveButton;
JButton dontSave;
public MyFrame() {
super("Crazy App");
setSize(400, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel row1 = new JPanel();
panicButton = new JButton("Panic");
dontPanic = new JButton("No Panic");
blameButton = new JButton("Blame");
newsButton = new JButton("News");
//adding first row
GridLayout grid1 = new GridLayout(4, 2, 10, 10);
setLayout(grid1);
FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.setLayout(flow1);
row1.add(panicButton);
row1.add(dontPanic);
row1.add(blameButton);
row1.add(newsButton);
add(row1);
//adding second row
JPanel row2 = new JPanel();
mediaButton = new JButton("Blame");
saveButton = new JButton("Save");
dontSave = new JButton("No Save");
GridLayout grid2 = new GridLayout(3, 2, 10, 10);
setLayout(grid2);
FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row2.setLayout(flow2);
row2.add(mediaButton);
row2.add(saveButton);
row2.add(dontSave);
add(row2);
setVisible(true);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
The original code set the layout for one panel on two separate occasions. For clarity, set it once in the constructor.
The 2nd layout specified 3 rows
Call pack() on the top-level container to have the GUI reduce to the minum sze needed for the components.
End result
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.*;
public class MyFrame17 extends JFrame {
JButton panicButton;
JButton dontPanic;
JButton blameButton;
JButton newsButton;
JButton mediaButton;
JButton saveButton;
JButton dontSave;
public MyFrame17() {
super("Crazy App");
setLayout(new GridLayout(2, 2, 10, 10));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel row1 = new JPanel();
panicButton = new JButton("Panic");
dontPanic = new JButton("No Panic");
blameButton = new JButton("Blame");
newsButton = new JButton("News");
FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.setLayout(flow1);
row1.add(panicButton);
row1.add(dontPanic);
row1.add(blameButton);
row1.add(newsButton);
add(row1);
//adding second row
JPanel row2 = new JPanel();
mediaButton = new JButton("Blame");
saveButton = new JButton("Save");
dontSave = new JButton("No Save");
FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row2.setLayout(flow2);
row2.add(mediaButton);
row2.add(saveButton);
row2.add(dontSave);
add(row2);
pack();
setVisible(true);
}
public static void main(String[] args) {
MyFrame17 frame = new MyFrame17();
}
}
Further tips
Don't extend frame, just use an instance of one.
Build the entire GUI in a panel which can then be added to a frame, applet, dialog..
When developing test classes, give them a more sensible name than MyFrame. A good word to add is Test, then think about what is being tested. This is about the layout of buttons, so ButtonLayoutTest might be a good name.
GUIs should be started on the EDT.

Categories