I tried creating simple GUI using JFrame with below code.
package sorting_array_gui;
package sorting_array_gui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
public class userwindow extends JFrame {
private static final long serialVersionUID = 1L;
public userwindow() {
super("A Programm to Sort Your Array");
setSize(1000,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
JPanel p1= new JPanel();
JButton b1= new JButton("Click Here");
p1.add(b1);
JTextField t1= new JTextField();
p1.add(t1);
JLabel l1= new JLabel("This is a Lable");
p1.add(l1);
add(p1,BorderLayout.CENTER);
}
}
When I added JTextfield , JPlane misbehaved and even JButton and JLabel stoped showing.
Why is that happening.
"When I added JTextfield , JPlane misbehaved and even JButton and JLabel stoped showing."
I don't get this behavior with your code. But you should note the below.
setVisible(true); should be that last thing you do after adding all components.
public userwindow() {
super("A Programm to Sort Your Array");
JPanel p1= new JPanel();
JButton b1= new JButton("Click Here");
p1.add(b1);
JTextField t1= new JTextField();
p1.add(t1);
JLabel l1= new JLabel("This is a Lable");
p1.add(l1);
add(p1,BorderLayout.CENTER);
pack(); <--- PACK frame
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true); <--- LAST
}
Also, you should set a size to your text field using the constructor that sets the column size
JTextField t1 = new JTextField(20);
Also, you should use pack() instead of setSize(). If you just pack(), everything should be visible, as the preferred sizes of all the components are respected.
Also note, if you want to add any other components to the JFrame you need to specify a BorderLayout position for each component, with no positions being used more than once.
See Laying out Components Within a Container
Related
I have to design a swing game where one side is a grid and the other side is somewhat of a display panel where I have several JLabels and a JButton. But no matter if I use setSize(); or setPrefferedSize (); or setBounds(); or even setPrefferedSize(new Dimension()); it will not become smaller but instead stretches the entirety of that section. Any JLabel/JButton aligned in the center takes up the entire center. How do I fix this?
This is the code to my class referencing to another class in the project containing the grid:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Scoreboard extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel scoreLabel;
JLabel coord;
JLabel title;
JButton quit;
public Scoreboard (int score){
setLayout(new BorderLayout());
setSize(490,400);
setPreferredSize(getSize());
setBackground(Color.BLUE);
title = new JLabel();
title.setIcon(new ImageIcon("C:\\Users\\Rachel\\Workspace\\Assignment2\\Planet1.png"));
title.setSize(200,200);
title.setHorizontalAlignment(SwingConstants.CENTER);
add (title,BorderLayout.NORTH);
scoreLabel = new JLabel("Score: "+Integer.toString(score));
scoreLabel.setSize(200,200);
scoreLabel.setBackground(Color.BLUE);
scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
scoreLabel.setFont(new Font("Source Sans Pro", Font.BOLD, 40));
scoreLabel.setForeground(Color.WHITE);
add(scoreLabel, BorderLayout.CENTER);
coord = new JLabel ("Click the aliens!");
coord.setSize(200,400);
coord.setBackground(Color.RED);
coord.setHorizontalAlignment(SwingConstants.CENTER);
coord.setFont(new Font("Source Sans Pro", Font.BOLD, 20));
coord.setForeground(Color.WHITE);
add(coord,BorderLayout.SOUTH);
JButton quit = new JButton ("Quit Game");
quit.setBounds(20,30,50,30);
quit.setHorizontalAlignment(SwingConstants.CENTER);
add(quit, BorderLayout.CENTER);
}
}
The following code does not solve the problem. Instead it shows some tips related to your question, as well as some others.
In general try to avoid "manually control" components layout, but use the right layout managers.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class Scoreboard extends JPanel{
JLabel scoreLabel;
JLabel coord;
JLabel title;
JButton quit;
private final int W = 490;
private final int H = 400;
public Scoreboard (int score){
setLayout(new BorderLayout());
setPreferredSize(new Dimension(W, H));
setBackground(Color.BLUE);
title = new JLabel("My Title");
//if you use images in your SO posted code use web links
//title.setIcon(new ImageIcon("C:\\Users\\Rachel\\Workspace\\Assignment2\\Planet1.png"));
//title.setSize(200,200); run the code without it and see it has no effect
title.setHorizontalAlignment(SwingConstants.CENTER);
add (title,BorderLayout.NORTH);
scoreLabel = new JLabel("Score: "+Integer.toString(score));
scoreLabel.setSize(200,200);
scoreLabel.setBackground(Color.BLUE);
scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
scoreLabel.setFont(new Font("Source Sans Pro", Font.BOLD, 40));
scoreLabel.setForeground(Color.WHITE);
//this is directly related to your question. You can't add 2 components
//to the center.
//instead add a JPanel to the center, apply a layout manager to it,
//and add scorelabel and quit button to that JPanel
add(scoreLabel, BorderLayout.CENTER);
coord = new JLabel ("Click the aliens!");
//coord.setSize(200,400); run the code without it and see it has no effect
coord.setBackground(Color.RED);
coord.setOpaque(true); //if you want the color show
coord.setHorizontalAlignment(SwingConstants.CENTER);
coord.setFont(new Font("Source Sans Pro", Font.BOLD, 20));
coord.setForeground(Color.WHITE);
add(coord,BorderLayout.SOUTH);
JButton quit = new JButton ("Quit Game");
//no need to set bounds. That is what the layout manager does
//quit.setBounds(20,30,50,30);
quit.setHorizontalAlignment(SwingConstants.CENTER);
add(quit, BorderLayout.CENTER);
}
//add main to your questions to make it runnable
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel panel = new Scoreboard(50);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
I'm using a tabbed pane and can't get the tab to show the GUI that I want. I plan to have different Panel objects for each different tab so that I can setup their layouts with more versatility. Right now I don't have any listeners or functions, and am strictly trying to get the components to show up.
Edit: Code is now in the question, not a link.
Here is the code for the UI for the "General":
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class GeneralGUI extends JPanel{
public GeneralGUI() {
JPanel topPanel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel subjectNum = new JLabel("Subject Street #:");
JLabel subjectStreet = new JLabel("Subject Street Name:");
JTable compTable = new JTable();
JTable subjectTable = new JTable();
JButton getRPT = new JButton("Get RPT file");
JButton getOrder = new JButton("Get Order/Contract");
JButton subjectDocs = new JButton("Get Subject Docs");
JButton compDocs = new JButton("Get Comp Docs");
panel1.add(subjectNum);
panel1.add(subjectStreet);
panel1.add(compTable);
panel2.add(getRPT);
panel2.add(getOrder);
panel2.add(subjectDocs);
panel2.add(compDocs);
panel2.add(subjectTable);
topPanel.add(panel1);
topPanel.add(panel2);
topPanel.setVisible(true);
}
}
Here is the code for the tabbed pane code:
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class AppraisalTabs extends JPanel {
public AppraisalTabs() {
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
GeneralGUI genGUI = new GeneralGUI();
// JComponent panel1 = makeTextPanel("General");
tabbedPane.addTab("General", genGUI);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JComponent panel2 = makeTextPanel("Docs");
tabbedPane.addTab("Docs", panel2);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
JComponent panel3 = makeTextPanel("Subject");
tabbedPane.addTab("Subject", panel3);
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
JComponent panel4 = makeTextPanel("Comps");
panel4.setPreferredSize(new Dimension(410, 300));
tabbedPane.addTab("Comps", panel4);
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
JComponent panel5 = makeTextPanel("Report");
panel4.setPreferredSize(new Dimension(800, 800));
tabbedPane.addTab("Report", panel5);
tabbedPane.setMnemonicAt(4, KeyEvent.VK_5);
//Add the tabbed pane to this panel.
add(tabbedPane);
//The following line enables to use scrolling tabs.
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Appraisal Helper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new AppraisalTabs(), BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
My problem is that once I run the code the tabbed pane shows up, as well as the correctly-titled tabs, but the "General" tab isn't showing anything at all. I tried to setup the buttons and everything in it but it's still blank.
Any ideas?
I'd like to create a main window frame with a BorderLayout that contains other layouts as its components.
How can I add, say, a FlowLayout to my BorderLayout's NORTH position?
Here's a little program that shows you:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class LayoutExample {
public static void main (String[] args) {
JFrame frame = new JFrame("Frame with BorderLayout");
frame.setLayout(new BorderLayout());
JPanel flow = new JPanel();
JLabel label = new JLabel("This is a flowlayout.");
flow.setBorder(new LineBorder(Color.BLACK));
flow.setLayout(new FlowLayout());
flow.add(label);
frame.add(flow, BorderLayout.NORTH);
frame.setSize(300,300);
frame.setVisible(true);
}
}
That's how it looks like at the end:
So for this program, I am trying to have a JToolBar on the left, and this spectrum panel on the right side. I am currently adding using a BorderLayout, but as you can see, the spectrum (in cyan) I add has a black border around it (the panel below). Why does it not fill the right side JPanel?
http://imgur.com/pTqMeGM
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class Spectrum extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
/*
*
*/
public Spectrum(){
JPanel main = new JPanel(new BorderLayout());
JPanel rightside = new JPanel();
JLabel spectrum = new JLabel("spectrum goes here");
JToolBar toolbar = new JToolBar(null, JToolBar.VERTICAL);
JButton button1 = new JButton("Icon 1");
JButton button2 = new JButton("Icon 2");
main.setBackground(Color.RED);
main.setPreferredSize(new Dimension(800, 500));
rightside.setBackground(Color.black);
spectrum.setPreferredSize(new Dimension(750,500));
spectrum.setOpaque(true);
spectrum.setBackground(Color.cyan);
toolbar.setPreferredSize(new Dimension(50, 500));
toolbar.setFloatable(false);
button1.setOpaque(true);
button2.setOpaque(true);
button1.setBackground(Color.blue);
button2.setBackground(Color.green);
toolbar.add(button1);
toolbar.add(button2);
rightside.add(spectrum);
main.add(toolbar, BorderLayout.WEST);
main.add(rightside, BorderLayout.EAST);
setContentPane(main);
pack();
setVisible(true);
}
}
I suggest you to do the following change.
main.add(rightside, BorderLayout.EAST);
to
main.add(rightside, BorderLayout.CENTER);
I want to make a simple program that will have one button and multiple fields. When I was planning this out in my head I wanted to use a gridlayout, or at least cent the button at first since I am learning. Here is what I have so far, my question that I am leading to is where do I put in my grid layout, or do I set the alignment center in the panel, frame or button?
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Normal {
public static void main(String[] args) {
JFrame frame = new JFrame("test");
JButton button = new JButton("why");
JPanel panel = new JPanel();
JTextField field= new JTextField();
//button
button.setSize(50, 50);
//Field
field.setSize(250, 25);
//Frame
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
frame.add(field);
frame.add(button);
}
}
Always add the components in a Container of the JFrame. Set the Layout of Container as GridLayout. For example You can change your code as follows:
import java.awt.GridLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Normal {
public static void main(String[] args) {
JFrame frame = new JFrame("test");
JButton button = new JButton("why");
JPanel panel = new JPanel();
JTextField field= new JTextField();
Container c = frame.getContentPane();
c.setLayout(new GridLayout(3,1));//Devides the container in 3 rows and 1 column
c.add(panel);//Add in first row
c.add(button);//Add in second row
c.add(field);//Add in third row
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In an approach where you extend your class from JFrame, you could simply set where you would like to add the components. If you have a panel with ex. a button, you could add it like this:
add(panel, BorderLayout.CENTER);
Hope this helps.