how to get the same look using using layout
package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* #author isslam
*/
public class MyFrameMain extends JFrame{
Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title){
super(title);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15,20);
reSoulte.setEditable(false);
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel panel = new JPanel(new GridLayout(5,1));
panel.add(nLabel);
panel.add(iLabel);
panel.add(rButton1);
panel.add(rButton2);
panel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2,2));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5,1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
JPanel jtaPanel = new JPanel(new GridLayout(5,1));
jtaPanel.add(reSoulte);
Container pane = getContentPane();
pane.setLayout(null);
pane.add(panel);
pane.add(bpanel);
pane.add(jtfPanel);
pane.add(jtaPanel);
Insets insets = pane.getInsets();
setSize(500 + insets.left + insets.right,500 + insets.top + insets.bottom);
Dimension size = panel.getPreferredSize();
panel.setBounds(0 + insets.left, 18 + insets.top,size.width, size.height);
size = bpanel.getPreferredSize();
bpanel.setBounds(0 + insets.left, 409 + insets.top,115+size.width, size.height);
size = jtfPanel.getPreferredSize();
jtfPanel.setBounds(180 + insets.left, 25 + insets.top,170 +size.width, size.height);
size = jtaPanel.getPreferredSize();
jtaPanel.setBounds(0 + insets.left, 150 + insets.top,265 +size.width, size.height);
exitButton.addActionListener(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
System.exit(0);
}
}
}
can you tell me how to replace absolute position to be borderlayout
The way I see your code:
"panel" is displayed in the top left
"jtfPanel" is displayed in the top right
"jtaPanel" is displayed in the center
"bPanel" is displayed in the bottom
BorderLayout has 3 vertical areas to work with NORTH, CENTER and SOUTH so I would suggest you combine "panel" and "jtfPanel" into a another panel, then you will have 3 panels to add to the BorderLayout. Something like:
JPanel north = new JPanel( new BorderLayout() );
north.add(panel, BorderLayout.WEST);
north.add(jtfPanel, BorderLayout.CENTER);
Container contentPane = getContentPane();
contentPane.add(north, BorderLayout.NORTH);
contentPane.add(jtaPanel, BorderLayout.CENTER);
contentPane.add(bPanel, BorderLayout.SOUTH);
The main point is that you can nest multiple panels to achieve your desired effect.
Here check this out. I added a main method to run it. I used EmptyBorders for needed spacing. I also grouped panels together which makes layouts easier to work with.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
/**
*
* #author isslam
*/
public class MyFrameMain extends JFrame {
// Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title) {
//super(title);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15, 45);
reSoulte.setEditable(false);
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version", false);
JRadioButton rButton2 = new JRadioButton("HW Type", false);
JRadioButton rButton3 = new JRadioButton("General", true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel panel = new JPanel(new GridLayout(5, 1));
panel.setBorder(new EmptyBorder(0, 0, 0, 75));
panel.add(nLabel);
panel.add(iLabel);
panel.add(rButton1);
panel.add(rButton2);
panel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2, 2));
bpanel.setBorder(new EmptyBorder(15, 0, 0, 0));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5, 1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
//JPanel jtaPanel = new JPanel(new GridLayout(5, 1));
//jtaPanel.add(reSoulte);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.setBorder(new EmptyBorder(0, 0, 20, 0));
topPanel.add(panel, BorderLayout.WEST);
topPanel.add(jtfPanel, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(new EmptyBorder(20, 10, 10, 10));
mainPanel.add(bpanel, BorderLayout.SOUTH);
mainPanel.add(reSoulte, BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.NORTH);
Container pane = getContentPane();
//pane.setLayout(null);
//pane.add(panel);
pane.add(mainPanel);
//pane.add(jtfPanel);
//Insets insets = pane.getInsets();
//setSize(500 + insets.left + insets.right, 500 + insets.top + insets.bottom);
//Dimension size = panel.getPreferredSize();
//panel.setBounds(0 + insets.left, 18 + insets.top, size.width, size.height);
//size = bpanel.getPreferredSize();
// bpanel.setBounds(0 + insets.left, 409 + insets.top, 115 + size.width, size.height);
// size = jtfPanel.getPreferredSize();
// jtfPanel.setBounds(180 + insets.left, 25 + insets.top, 170 + size.width, size.height);
//size = jtaPanel.getPreferredSize();
//jtaPanel.setBounds(0 + insets.left, 150 + insets.top, 265 + size.width, size.height);
exitButton.addActionListener(new ButtonWatcher());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
MyFrameMain frame = new MyFrameMain("Title");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
//frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
private class ButtonWatcher implements ActionListener {
public void actionPerformed(ActionEvent a) {
System.exit(0);
}
}
}
Related
Below is a picture of the basic interface I want created. Everything works fine except for the following:-
The JScrollPane isn't appearing when I try inserting lots of text
in the InformationDisplay JTextArea and I don't get why
The buttons I inserted in the unitButtonPanel in the availableUnits JTextArea isn't appearing at all
This is the part in my code that tackles point #1
InfoPanel with informationDisplay TextArea and JScrollPane
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
JTextArea informationDisplay= new JTextArea();
JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollbar1.setSize( 20, 20 );
informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));
infoPanel.add(scrollbar1);
add(infoPanel, BorderLayout.SOUTH);
This is the part in my code that tackles point #2
Adding a textPanel 3 rows, 1 column to encapsulate 3 TextAreas
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3, 1, 4, 4));
TextArea1 "availableUnits" 2 rows and 2 columns to encapsulate 4 buttons
JTextArea availableUnits = new JTextArea(2,2);
//availableUnits.setPreferredSize(new Dimension(200, 200));
JPanel unitButtonPanel = new JPanel();
unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
JButton ambulanceUnit = new JButton("Ambulance");
JButton diseaseControlUnit = new JButton ("diseaseControlUnit");
JButton gasControlUnit = new JButton("gasControlUnit");
JButton fireTruck = new JButton("fireTruck");
unitButtonPanel.add(ambulanceUnit);
unitButtonPanel.add(diseaseControlUnit);
unitButtonPanel.add(gasControlUnit);
unitButtonPanel.add(fireTruck);
availableUnits.add(unitButtonPanel);
textPanel.add(availableUnits);
This is the full code ready for compilation:-
import java.awt.*;
import javax.swing.*;
public class GameView extends JFrame {
private JPanel buttonPanel;
private JPanel infoPanel;
private JTextArea informationDisplay;
private JTextArea availableUnits;
private JPanel unitButtonPanel;
private JPanel disasterPanel;
private JTextArea disasterDisplay;
private JButton ambulanceUnit;
private JButton diseaseControlUnit;
private JButton gasControlUnit;
private JButton fireTruck;
public static void main(String[] args) {
new GameView();
}
public GameView() {
setSize(1000, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int xPos = (dim.width/2) - (this.getWidth()/2);
int yPos = (dim.height/2) - (this.getHeight()/2);
this.setLocation(xPos,yPos);
JPanel title = new JPanel();
JLabel label1 = new JLabel("Invaded City");
title.add(label1);
add(title, BorderLayout.NORTH);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(10, 10, 2, 2));
for (int i = 0; i < 100; i++) {
JButton mapButton = new JButton();
addButton(mapButton);
}
add(buttonPanel, BorderLayout.CENTER);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3, 1, 4, 4));
availableUnits = new JTextArea(2,2);
//availableUnits.setPreferredSize(new Dimension(200, 200));
unitButtonPanel = new JPanel();
unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
ambulanceUnit = new JButton("Ambulance");
diseaseControlUnit = new JButton ("diseaseControlUnit");
gasControlUnit = new JButton("gasControlUnit");
fireTruck = new JButton("fireTruck");
unitButtonPanel.add(ambulanceUnit);
unitButtonPanel.add(diseaseControlUnit);
unitButtonPanel.add(gasControlUnit);
unitButtonPanel.add(fireTruck);
availableUnits.add(unitButtonPanel);
textPanel.add(availableUnits);
JTextArea respondingUnits = new JTextArea(2,2);
//respondingUnits.setPreferredSize(new Dimension(200, 200));
textPanel.add(respondingUnits);
JTextArea treatingUnits = new JTextArea(2,2);
//treatingUnits.setPreferredSize(new Dimension(200, 200));
textPanel.add(treatingUnits);
add(textPanel, BorderLayout.EAST);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
JTextArea informationDisplay= new JTextArea();
JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollbar1.setSize( 20, 20 );
informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));
infoPanel.add(scrollbar1);
add(infoPanel, BorderLayout.SOUTH);
disasterPanel = new JPanel();
disasterPanel.setLayout(new BorderLayout());
disasterDisplay = new JTextArea();
disasterDisplay.setPreferredSize(new Dimension(getWidth(), 200));
disasterPanel.add(disasterDisplay);
add(disasterPanel, BorderLayout.WEST);
pack();
setVisible(true);
}
public void addButton(JButton b) {
buttonPanel.add(b);
}
}
Output:-
I'm trying to get the top right square to be the same width as the two below it and I'd like to get the JTextArea below to match width as well. Any ideas?
It seems like no matter what I set the sizes to it's doing whatever it wants. For instance the Output JTextArea is set to just one column. The top image is (700x250) and the two half-images are (350x250).
public class MyApplet extends Applet{
private static final long serialVersionUID = 1L;
private JTextArea input_data;
private JTextArea input_jmax;
private JTextArea input_gibbs;
private JTextArea input_burnin;
private JTextArea output_text;
private JLabel output_graph;
private JLabel output_burn1;
private JLabel output_burn2;
private static Graphics g=null;
public void init () {
//INPUT
this.input_data = new JTextArea("Enter Data", 30, 30);
JScrollPane data_pane= new JScrollPane(input_data);
this.input_jmax = new JTextArea("Polya-Tree Levels", 1, 30);
this.input_gibbs = new JTextArea("Gibbs Iterates", 1, 30);
this.input_burnin = new JTextArea("Burnin", 1, 30);
//OUTPUT
Dimension D;
D = new Dimension(700, 250);
Image start;
this.output_text = new JTextArea("####################Output####################",15,1);
this.output_text.setEditable(false);
JScrollPane output_pane= new JScrollPane(output_text);
this.output_burn1 = new JLabel();
D = new Dimension(345,250);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,250);
output_burn1.setIcon(new ImageIcon(start));
this.output_burn2 = new JLabel();
D = new Dimension(345, 250);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,250);
output_burn2.setIcon(new ImageIcon(start));
this.output_graph = new JLabel();
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,700,250);
output_graph.setIcon(new ImageIcon(start));
//BUTTON
JButton b = new JButton("Process Data");
//set size
setSize(1200, 600);
setBackground(Color.lightGray);
JPanel burninPanel = new JPanel();
burninPanel.setLayout(new BoxLayout(burninPanel, BoxLayout.X_AXIS));
burninPanel.add(output_burn1);
burninPanel.add(Box.createHorizontalStrut(10));
burninPanel.add(output_burn2);
/*
//Create Input Side
* */
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
inputPanel.setBackground(Color.LIGHT_GRAY);
inputPanel.add(data_pane);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_jmax);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_gibbs);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_burnin);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(b);
//Create Output Side
JPanel outputPanel = new JPanel();
outputPanel.setBackground(Color.lightGray);
outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
outputPanel.add(output_graph);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(burninPanel);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(output_pane);
this.setLayout(new GridLayout(1, 2));
this.setVisible(true);
GroupLayout layout = new GroupLayout(this);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
this.setLayout(layout);
this.setVisible(true);
// specify that action events sent by the
// button or the input TextField should be handled
// by the same CapitalizerAction object
Multiplicity ca = new Multiplicity(input_data, input_jmax, input_gibbs, input_burnin, output_text,output_graph);
b.addActionListener(ca);
//this.input.addActionListener(ca);
}
}
It turns out that you can't add an image of the same size to the stack. Instead of placing all the items an "outputPanel" I put each one in it's own panel and added all the panels to the outputPanel. Such a stupid thing!
public class MyApplet extends Applet{
private static final long serialVersionUID = 1L;
private JTextArea input_data;
private JTextArea input_jmax;
private JTextArea input_gibbs;
private JTextArea input_burnin;
private JTextArea output_text;
private JLabel output_graph;
private JLabel output_burn1;
private JLabel output_burn2;
private static Graphics g=null;
public void init () {
//INPUT
this.input_data = new JTextArea("Data", 30, 30);
JScrollPane data_pane= new JScrollPane(input_data);
this.input_jmax = new JTextArea("Polya-Tree Levels", 1, 30);
this.input_gibbs = new JTextArea("Gibbs Iterates", 1, 30);
this.input_burnin = new JTextArea("Burn-in", 1, 30);
//OUTPUT
Dimension D;
Image start;
this.output_burn1 = new JLabel();
D = new Dimension(345,200);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,200);
output_burn1.setIcon(new ImageIcon(start));
this.output_burn2 = new JLabel();
D = new Dimension(345, 200);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,200);
output_burn2.setIcon(new ImageIcon(start));
this.output_graph = new JLabel();
D = new Dimension(700, 200);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,700,200);
output_graph.setIcon(new ImageIcon(start));
//BUTTON
JButton b = new JButton("Process Data");
//set size
setSize(1200, 600);
setBackground(Color.lightGray);
//Create Input Side
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.LIGHT_GRAY);
buttonPanel.add(b);
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
inputPanel.setBackground(Color.LIGHT_GRAY);
inputPanel.add(data_pane);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_jmax);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_gibbs);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_burnin);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(buttonPanel);
//Create Output Side
JPanel estPanel = new JPanel();
estPanel.setBackground(Color.LIGHT_GRAY);
estPanel.add(output_graph);
JPanel burninPanel = new JPanel();
burninPanel.setBackground(Color.LIGHT_GRAY);
burninPanel.setLayout(new BoxLayout(burninPanel, BoxLayout.X_AXIS));
burninPanel.add(output_burn1);
burninPanel.add(Box.createHorizontalStrut(10));
burninPanel.add(output_burn2);
this.output_text = new JTextArea("",15,1);
this.output_text.setEditable(false);
JScrollPane output_pane= new JScrollPane(output_text);
JPanel outputPanel = new JPanel();
outputPanel.setBackground(Color.lightGray);
outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
outputPanel.add(estPanel);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(burninPanel);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(output_pane);
this.setVisible(true);
GroupLayout layout = new GroupLayout(this);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
this.setLayout(layout);
this.setVisible(true);
// specify that action events sent by the
// button or the input TextField should be handled
// by the same CapitalizerAction object
Multiplicity ca = new Multiplicity(input_data, input_jmax, input_gibbs, input_burnin, output_text,output_graph,output_burn1,output_burn2);
b.addActionListener(ca);
}
}
I have the following GUI codded up but I would like to increase the length of the scroll bar on the right side.
Any Idea How to do this?
// test class that implements the gui stuff.
public class testing
{
//variables
private JFrame f = new JFrame("GUI TEST");
private JPanel p = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3 = new JPanel();
private JPanel p4 = new JPanel();
private JPanel p5 = new JPanel();
private JPanel p6 = new JPanel();
private JPanel p7 = new JPanel();
private JPanel p8 = new JPanel();
private JPanel p9 = new JPanel();
private JPanel p10 = new JPanel();
private JPanel p11 = new JPanel();
private JButton b1 = new JButton("Button");
private JTextField tf1 = new JTextField(" ");
private JTextField tf2 = new JTextField(" ");
private JTextField tf3 = new JTextField(" ");
private JTextArea ta1 = new JTextArea(10,45);
private JLabel label1 = new JLabel("Label 1");
private JLabel label2 = new JLabel("Label 2");
private JLabel label3 = new JLabel("Label 3");
private JLabel label4 = new JLabel("Label 4");
private JScrollBar sb1 = new JScrollBar();
//class constructor
public testing()
{
gui();
}
public void gui()
{
//change length of scroll bar
f.setVisible(true);
f.setSize(600,300);
p.add(label1);
p.add(tf1);
p2.add(label2);
p2.add(tf2);
p3.add(label3);
p3.add(tf3);
p4.add(sb1);
p4.add(label4);
p5.add(ta1);
p6.add(b1);
p4.setBackground(Color.GRAY);
p9.setBackground(Color.GRAY);
p10.setBackground(Color.GRAY);
p11.setBackground(Color.GRAY);
p9.add(p);
p9.add(p2);
p9.add(p3);
p10.add(p5);
p11.add(p6);
//adds panels to frames
f.add(p4, BorderLayout.EAST);
f.add(p9, BorderLayout.NORTH);
f.add(p10, BorderLayout.CENTER);
f.add(p11, BorderLayout.PAGE_END);
}
public static void main(String[] args)
{
new testing();
}
Ordinarilly, you'd simply add your JTextArea to a JScrollPane, which handles the resizing behavior for you.
f.add(new JScrollPane(ta1), BorderLayout.CENTER);
For demonstration purposes, you can override the getPreferredSize() method of the JScrollBar to see the effect.
private JScrollBar sb1 = new JScrollBar(){
#Override
public Dimension getPreferredSize() {
return new Dimension(
super.getPreferredSize().width, ta1.getPreferredSize().height);
}
};
In addition,
Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
Use the appropriate constructor to establish the desired initial size of text components.
Use an appropriate layout to get the desired resizing behavior.
As tested:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Testing {
//variables
private JFrame f = new JFrame("GUI TEST");
private JPanel p = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3 = new JPanel();
private JPanel p4 = new JPanel();
private JPanel p5 = new JPanel();
private JPanel p6 = new JPanel();
private JPanel p9 = new JPanel();
private JPanel p10 = new JPanel();
private JPanel p11 = new JPanel();
private JButton b1 = new JButton("Button");
private JTextField tf1 = new JTextField(12);
private JTextField tf2 = new JTextField(12);
private JTextField tf3 = new JTextField(12);
private JTextArea ta1 = new JTextArea(10, 45);
private JLabel label1 = new JLabel("Label 1");
private JLabel label2 = new JLabel("Label 2");
private JLabel label3 = new JLabel("Label 3");
private JLabel label4 = new JLabel("Label 4");
private JScrollBar sb1 = new JScrollBar(){
#Override
public Dimension getPreferredSize() {
return new Dimension(super.getPreferredSize().width, ta1.getPreferredSize().height);
}
};
//class constructor
public Testing() {
gui();
}
public void gui() {
p.add(label1);
p.add(tf1);
p2.add(label2);
p2.add(tf2);
p3.add(label3);
p3.add(tf3);
p4.add(sb1);
p4.add(label4);
p5.add(ta1);
p6.add(b1);
p4.setBackground(Color.GRAY);
p9.setBackground(Color.GRAY);
p10.setBackground(Color.GRAY);
p11.setBackground(Color.GRAY);
p9.add(p);
p9.add(p2);
p9.add(p3);
p10.add(p5);
p11.add(p6);
//adds panels to frames
f.add(p4, BorderLayout.EAST);
f.add(p9, BorderLayout.NORTH);
f.add(p10, BorderLayout.CENTER);
f.add(p11, BorderLayout.PAGE_END);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Testing();
});
}
}
I have some problem , i want my jpanel to look like this
but after codING i had this view and i didnt find the solution ,
i need some helps please.
i put the radio button inside jScrollPan
//listmateriel is a list (vector) of radio button ----------
//imagebox is jpanel contains jradio buttons and the image ---------
//gridlayoutPanel2 is Jpanel contains the two text fields and jlabel and a JComboBox
package tpjava;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
i.......
public class Frame extends JFrame implements ActionListener {
private static final int Haut = 550;
private static final int Larg = 720;
private final JPanel utilisateur;
private final JButton recherche;
private final JRadioButton radioButton,radioButton2;
private Vector listmateriel;
private final JLabel label;
public Frame()
{
super("Gestion des materiels");
setSize(Larg,Haut);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane menu = new JTabbedPane();
JPanel emprunt = new JPanel();
emprunt.setPreferredSize(new Dimension(Larg, Haut ));
emprunt.setLayout(new BoxLayout(emprunt, BoxLayout.Y_AXIS));
emprunt.setBorder(BorderFactory.createEmptyBorder(20,20, 20, 20));
JPanel emp1 = new JPanel();
emp1.setPreferredSize(new Dimension(Larg-40, 200 ));
emp1.setLayout(new BorderLayout());
JLabel title2 = new JLabel("Nouvel emprunt:");
title2.setBorder(BorderFactory.createEmptyBorder(20,20,0,0));
JPanel box2 = new JPanel();
box2.setPreferredSize(new Dimension(Larg, 40 ));
box2.setLayout(new BoxLayout(box2, BoxLayout.X_AXIS));
box2.add(title2);
box2.add(Box.createHorizontalStrut(650));
emprunt.add(box2);
JLabel label3 = new JLabel("test");
JButton dispo = new JButton("Disponibilité");
dispo.addActionListener((ActionListener) this);
dispo.setPreferredSize(new Dimension(200, 30));
JPanel box1 = new JPanel();
box1.setLayout(new BoxLayout(box1, BoxLayout.X_AXIS));
box1.add(dispo);
box1.add(Box.createHorizontalStrut(450));
box1.add(label3);
emp1.add(box1,BorderLayout.SOUTH);
listmateriel = new Vector();
for(int k =0 ;k <10 ; k++)
listmateriel .add(new JRadioButton(res.getString("nom"+k)));
......
ImageIcon newIcon = new ImageIcon(bi);
JLabel picture = new JLabel(newIcon);
picture.setBorder(BorderFactory.createEmptyBorder(0,450,0,0));
JPanel jplRadio = new JPanel();
jplRadio.setLayout(new GridLayout(0, 1));
for(int i = 0 ; i < listmateriel.size() ;i++)
{
jplRadio.add((Component) listmateriel.elementAt(i));
}
JPanel imagebox = new JPanel();
imagebox.setLayout(new BorderLayout());
JScrollPane scroll3 = new JScrollPane(jplRadio);
scroll3.setPreferredSize(new Dimension(200, 30));
imagebox.add(scroll3, BorderLayout.WEST);
imagebox.add(picture, BorderLayout.CENTER);
emp1.add(imagebox,BorderLayout.CENTER);
JPanel box3 = new JPanel();
box3.setLayout(new BoxLayout(box3, BoxLayout.X_AXIS));
JLabel title3 = new JLabel("Remplir les champs suivants:");
box3.add(title3);
box3.add(Box.createHorizontalStrut(650));
JPanel gridlayoutPanel2 = new JPanel();
........
JPanel util5 = new JPanel();
util5.setLayout(new GridLayout(0,1));
util5.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel resul2 = new JLabel("bsfgbsdg");
JScrollPane scroll2 = new JScrollPane();
util5.add(resul2);
emprunt.add(emp1);
emprunt.add(box3);
emprunt.add(gridlayoutPanel2);
emprunt.add(util5);
menu.add("emprunts",emprunt);
//---------------------
getContentPane().add(menu);
pack();
setVisible(true);
}
}
GridLayout(0, 1) instead of BoxLayout is the solution thank you #trashgod
So I am trying to get a JFrame to display a JPanel that has 5 other JPanels in it. I dont have any syntax errors and all that displays is a very small screen. I have been at this all day and have yet to find a solution.
public class addressPanel extends JPanel {
private JTextField nameT;
private JTextField addressT;
private JTextField cityT;
private JTextField stateT;
private JTextField zipCodeT;
private JTextField phoneNumberT;
private JLabel Title;
private JLabel addressTitle;
private JLabel nameL;
private JLabel addressL;
private JLabel stateL;
private JLabel cityL;
private JLabel zipCodeL;
private JLabel phoneNumberL;
private JLabel orderType;
private JRadioButton takeOut;
private JRadioButton delivery;
private JButton clear;
private JButton submit;
private JPanel addressTextPanel;
private JPanel addressLabelPanel;
private JPanel orderTypePanel;
private JPanel titlePanel;
private JPanel buttonsPanel;
public JPanel addressTextPanel() {
nameT = new JTextField(1);
addressT = new JTextField(2);
cityT = new JTextField(3);
stateT = new JTextField(4);
zipCodeT = new JTextField(5);
phoneNumberT = new JTextField(6);
Font font = new Font(Font.SERIF, Font.PLAIN, 24);
nameT.setFont(font);
addressT.setFont(font);
cityT.setFont(font);
stateT.setFont(font);
zipCodeT.setFont(font);
phoneNumberT.setFont(font);
JPanel addressTextPanel = new JPanel();
addressTextPanel.setPreferredSize(new Dimension(125, 250));
addressTextPanel.setLayout(new BoxLayout(addressTextPanel, BoxLayout.Y_AXIS));
addressTextPanel.add(nameT);
addressTextPanel.add(addressT);
addressTextPanel.add(cityT);
addressTextPanel.add(stateT);
addressTextPanel.add(zipCodeT);
addressTextPanel.add(phoneNumberT);
return addressTextPanel;
}
public JPanel addressLabelPanel() {
nameL = new JLabel("Name:");
addressL = new JLabel("Address:");
cityL = new JLabel("City:");
zipCodeL = new JLabel("Zip Code:");
stateL = new JLabel("State:");
phoneNumberL = new JLabel("Phone Number:");
nameL.setFont(nameL.getFont().deriveFont(24.0f));
addressL.setFont(addressL.getFont().deriveFont(24.0f));
cityL.setFont(cityL.getFont().deriveFont(24.0f));
zipCodeL.setFont(zipCodeL.getFont().deriveFont(24.0f));
stateL.setFont(stateL.getFont().deriveFont(24.0f));
phoneNumberL.setFont(phoneNumberL.getFont().deriveFont(24.0f));
JPanel addressLabelPanel = new JPanel();
addressLabelPanel.setPreferredSize(new Dimension(125, 250));
addressLabelPanel.setLayout(new BoxLayout(addressLabelPanel, BoxLayout.Y_AXIS));
addressLabelPanel.add(nameL);
addressLabelPanel.add(addressL);
addressLabelPanel.add(cityL);
addressLabelPanel.add(stateL);
addressLabelPanel.add(zipCodeL);
addressLabelPanel.add(phoneNumberL);
return addressLabelPanel;
}
public JPanel orderTypePanel() {
orderType = new JLabel("Order Type:");
takeOut = new JRadioButton("Take Out");
delivery = new JRadioButton("Delivery");
orderType.setFont(takeOut.getFont().deriveFont(24.0f));
takeOut.setFont(takeOut.getFont().deriveFont(24.0f));
delivery.setFont(delivery.getFont().deriveFont(24.0f));
JPanel orderTypePanel = new JPanel();
orderTypePanel.setPreferredSize(new Dimension(250, 125));
orderTypePanel.setLayout(new BoxLayout(orderTypePanel, BoxLayout.Y_AXIS));
orderTypePanel.add(orderType);
orderTypePanel.add(takeOut);
orderTypePanel.add(delivery);
return orderTypePanel;
}
public JPanel titlePanel() {
Title = new JLabel("Pizza Order Form");
addressTitle = new JLabel("Address");
Title.setFont(Title.getFont().deriveFont(36.0f));
addressTitle.setFont(addressTitle.getFont().deriveFont(36.0f));
JPanel titlePanel = new JPanel();
titlePanel.setPreferredSize(new Dimension(500, 100));
titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
titlePanel.add(Title);
titlePanel.add(addressTitle);
return titlePanel;
}
public JPanel buttonsPanel() {
clear = new JButton("Clear");
submit = new JButton("Submit");
clear.setFont(clear.getFont().deriveFont(24.0f));
submit.setFont(submit.getFont().deriveFont(24.0f));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setPreferredSize(new Dimension(500, 100));
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(clear);
buttonsPanel.add(submit);
return buttonsPanel;
}
public addressPanel() {
JPanel addressParent = new JPanel(new BorderLayout());
addressParent.add(new titlePanel(), BorderLayout.NORTH);
addressParent.add(new orderTypePanel(), BorderLayout.WEST);
addressParent.add(new addressLabelPanel(), BorderLayout.CENTER);
addressParent.add(new addressTextPanel(), BorderLayout.EAST);
addressParent.add(new buttonsPanel(), BorderLayout.SOUTH);
}
public static void main(String[] args) {
// Create Main Panel
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.getContentPane().add(new addressPanel());
// Color background = new Color(238,233,191);
// frame.getContentPane().setBackground(background);
frame.pack();
frame.setVisible(true);
}
}
Read your code. The program creates a JFrame. It creates an instance of addressPanel (which should be named AddressPanel). ANd it adds this addressPanel instance to the frame conent pane.
Now what is added to the addressPanel? Nothing:
public addressPanel()
{
JPanel addressParent = new JPanel(new BorderLayout());
addressParent.add (new titlePanel(), BorderLayout.NORTH);
addressParent.add (new orderTypePanel(), BorderLayout.WEST);
addressParent.add (new addressLabelPanel(), BorderLayout.CENTER);
addressParent.add (new addressTextPanel(), BorderLayout.EAST);
addressParent.add (new buttonsPanel(), BorderLayout.SOUTH);
}
The constructor of addressPanel creates another panel (addressParent), adds plenty of things to this addressParent panel, but doesn't add anything to this, the addressPanel. So the addressPanel is empty.
Please respect the Java naming conventions to make your code readable. Classes start with an uppercase letter.