import java.awt.*;
import javax.swing.*;
public class guiAs {
public static void main (String args [] )
{
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension (300,300));
frame.setTitle("Calculator");
JPanel panel = new JPanel();
// JLabel label = new JLabel();
//creating num1
JPanel panel2 = new JPanel();
JLabel lable1 = new JLabel("Number 1 ");
JTextField tf1 = new JTextField();
//creating num2
JLabel lable2 = new JLabel("Number 2 ");
JTextField tf2 = new JTextField();
//creating result
JPanel panel3 = new JPanel();
JLabel lable3 = new JLabel("Result: ");
JTextField tf3 = new JTextField(10);
//creating button
JButton Add= new JButton("Add");
JButton Subtract = new JButton("Subtract");
JButton Multiply = new JButton("Multiply");
JButton Division = new JButton("Division");
//creating num1
panel2.add(lable1);
panel2.add(tf1);
//creating num2
panel2.add(lable2);
panel2.add(tf2);
//creating result
panel3.add(lable3);
panel3.add(tf3);
//creating buttons
panel.add(Add);
panel.add(Subtract);
panel.add(Multiply);
panel.add(Division);
// frame.getContentPane().add(BorderLayout.WEST, panel3);
//creating Box Layout for num1 and num2
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
frame.add(panel2);
//creating Box Layout for buttons
BoxLayout layout1 = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(layout1);
frame.setLayout(new FlowLayout());
frame.add(panel);
// Add.setAlignmentX(Component.RIGHT_ALIGNMENT);
// panel.add(Add);
// frame.getContentPane().add(BorderLayout.SOUTH, panel);
//creating Border Layout for num1 and num2
frame.getContentPane().add(BorderLayout.WEST, panel2);
//creating Border Layout for Buttons
frame.getContentPane().add(BorderLayout.EAST, panel);
//creating Box Layout for Result
frame.getContentPane().add(BorderLayout.SOUTH, panel3);
frame.setVisible(true);
}
}
this is my code, and I have no idea to make the text field next to the numbers. i try several times but does not comes together. whenever i add something the panels moves
its work only with the result but for the number doesn't work
here i used Box Layout for buttons and number 1 and number 2:
You were very close to solving it.
Replace this line
JPanel panel2 = new JPanel();
With this line
JPanel panel2 = new JPanel(new GridLayout(2, 2));
Then, get rid of these 3 lines
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
Now your code looks like this
Related
This question already has answers here:
Providing white space in a Swing GUI
(6 answers)
Closed 7 years ago.
With the code below I am making a JPanel with JTextFields and JLabels and adding that panel to another JPanel. How can I adjust the spacing between the JTextFields on infoPanel?
I have tried GridBagLayout and GridLayout with different and undesired results. The way it is right now at least gets them aligned vertically, but I can't seem to add space above and below them. Any gurus on this subject able to help out?
public DrawPanelMain() {
JPanel btnPanel = new JPanel(); //Creates a new Panel for the buttons
JPanel infoPanel = new JPanel();
JPanel fields = new JPanel();
//Text boxes for infoPanel
JTextField textField1 = new JTextField(20);
JTextField textField2 = new JTextField(20);
JTextField textField3 = new JTextField(20);
JTextField textField4 = new JTextField(20);
JTextField textField5 = new JTextField(20);
JTextField textField6 = new JTextField(20);
//JLabels for infoPanel
JLabel jLabel1 = new JLabel("Serial Number: ");
JLabel jLabel2 = new JLabel("Information: ");
JLabel jLabel3 = new JLabel("Information: ");
JLabel jLabel4 = new JLabel("Information: ");
JLabel jLabel5 = new JLabel("Information: ");
JLabel jLabel6 = new JLabel("Information: ");
//These are the buttons that will be added to the btnPanel
btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
btnPanel.add(new JButton(new PushConfigAction("Push Config")));
btnPanel.add(new JButton(new ActivateAllAction("Activate All")));
btnPanel.add(new JButton(new DeactivateAllAction("Deactivate All")));
//Fields that will be added to infoPanel
fields.add(jLabel1);
fields.add(textField1);
fields.add(jLabel2);
fields.add(textField2);
fields.add(jLabel3);
fields.add(textField3);
fields.add(jLabel4);
fields.add(textField4);
fields.add(jLabel5);
fields.add(textField5);
fields.add(jLabel6);
fields.add(textField6);
//Sets border padding for the infoPanel
fields.setBorder(new EmptyBorder(20, 20, 0, 20));
//Draws border for the infoPanel
infoPanel.setBorder(BorderFactory.createRaisedBevelBorder());
//Sets layout for the fields panel
fields.setLayout(new GridLayout(6, 1));
//Add fields to infoPanel
infoPanel.add(fields);
//Add panels to tabbedPane
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
add(infoPanel, BorderLayout.EAST);
}
create a compound border
field.setBorder(BorderFactory.createCompoundBorder(
field.getBorder(),
BorderFactory.createEmptyBorder(int top, int left, int bottom, int right)));
Or put some insets
field.setMargin(new java.awt.Insets(int top, int left, int bottom, int right));
I hava a problem with the applet when it is converted into an executable. the applet has three buttons with text as shown in pic_1 below, and when it is converted into an executable, the text of the three buttons disappears and the buttons becomes smalle as shown in pic_2 below.
I downloaded some applications that generates the executable file from a .jar files, and the result after using these applications is the same, the applet runs but with no buttons.
please provide any suggestions to solve this issue.
code_init() method_:
public void init () {
frame = new JFrame ();
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
/* Erzeuge MenuBar*/
menubar = new JMenuBar();
frame.setJMenuBar(menubar);
einstellungen = new JMenu ("Einstellungen");
datei = new JMenuItem ("Datei");
datei.addActionListener(this);
communication = new JMenuItem ("Kommunikation");
communication.addActionListener(this);
simulation = new JMenuItem ("Simulation");
simulation.addActionListener(this);
menubar.add(einstellungen);
einstellungen.add(datei);
einstellungen.add(communication);
einstellungen.add(simulation);
/*Erzeuge Layout */
panel1 = new JPanel ();
top = new JPanel ();
center = new JPanel ();
down = new JPanel ();
bottom = new JPanel ();
frame.add(panel1); //general Panel
panel1.setLayout(new BorderLayout ()); //Separate areas in BorderLayout
panel1.add(top, BorderLayout.NORTH);
panel1.add(center, BorderLayout.CENTER);
panel1.add(bottom, BorderLayout.SOUTH);
Lspeed = new JLabel ("Geschwindigkeit: ");
Lspeedanzeige = new JLabel (" ");
LineBorder lBorder = new LineBorder(new Color(100, 100, 100));
Lspeedanzeige.setBorder(lBorder);
JLabel Lfueller = new JLabel (" ");
Ldrive = new JLabel ("Motordrehzahl: ");
Ldriveanzeige = new JLabel (" ");
LineBorder rBorder = new LineBorder(new Color(100, 100, 100));
Ldriveanzeige.setBorder(rBorder);
top.setLayout(new FlowLayout ()); //add FlowLayout to Panel top
top.add(Lspeed);
top.add(Lspeedanzeige);
top.add(Lfueller);
top.add(Ldrive);
top.add(Ldriveanzeige);
simulationarea = new JTextArea ("Gesendete Daten:");
JScrollPane scrollPane = new JScrollPane(simulationarea);
simulationarea.setPreferredSize(new Dimension(5, 100));
simulationarea.setLineWrap(true);
simulationarea.setWrapStyleWord(true);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(5, 100));
scrollPane.setVisible(true);
simulationarea.setEditable(true);
simulationarea.setEnabled(true);
int pos = simulationarea.getCaretPosition();
simulationarea.insert("file logs", pos+17);
center.setLayout(new BorderLayout ());
center.add(simulationarea, BorderLayout.NORTH); //add JTextfield simulationarea to Panel center
center.add(down, BorderLayout.SOUTH); //add Panel down zu Panel Center
Bplay = new JButton ("Play");
Bplay.setPreferredSize(new Dimension(60, 50));
Bplay.addActionListener(this);
Bbreak = new JButton ("Pause");
Bbreak.addActionListener(this);
Bstop = new JButton ("Stop");
Bstop.addActionListener(this);
down.setLayout(new FlowLayout ()); //add FlowLayout to Panel down
down.add(Bplay);
down.add(Bbreak);
down.add(Bstop);
statusarea = new JTextArea ("Nicht verbunden");
statusarea.setForeground(Color.RED);
statusarea.setPreferredSize(new Dimension(550, 50));
bottom.add(statusarea);
frame.setVisible(true);
System.out.println("init");
}//init
Pic_1
pic_2
Below is the image that will clearly define my problem while using GridLayout
private void init() {
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField loginUNTextField = new JTextField();
JTextField loginPTextField = new JTextField();
JTextField registerUNTextField = new JTextField();
JTextField registerPTextField = new JTextField();
JTextField registerETextField = new JTextField();
JButton login = new JButton("Login");
JButton register = new JButton("Register");
JLabel loginUsername = new JLabel("Username");
JLabel loginPassword = new JLabel("Password");
JLabel registerUsername = new JLabel("Username");
JLabel registerPassword = new JLabel("Password");
JLabel registerEmail = new JLabel("Email");
JPanel loginUNPanel = new JPanel(new GridLayout(3,2));
loginUNPanel.add(loginUsername);
loginUNPanel.add(loginUNTextField);
loginUNPanel.add(loginPassword); // Cause Problem
loginUNPanel.add(loginPTextField);
loginUNPanel.add(new JLabel("")); // Cause Problem
loginUNPanel.add(login);
JPanel registerUNPanel = new JPanel(new GridLayout(4,2));
registerUNPanel.add(registerUsername);
registerUNPanel.add(registerUNTextField);
registerUNPanel.add(registerPassword);
registerUNPanel.add(registerPTextField);
registerUNPanel.add(registerEmail);
registerUNPanel.add(registerETextField);
registerUNPanel.add(new JLabel(""));
registerUNPanel.add(register);
loginUNPanel.add(loginPassword);
JPanel main = new JPanel(new GridLayout(1,2));
main.add(loginUNPanel);
main.add(registerUNPanel);
frame.add(main);
frame.pack();
frame.setVisible(true);
}
Desire Result:
UserName | TextField
Passowrd | TextField
| LoginButon
Please let me know where I am worng using GridLayout.
You're adding a component twice to the container.
JPanel loginUNPanel = new JPanel(new GridLayout(3,2));
loginUNPanel.add(loginUsername);
loginUNPanel.add(loginUNTextField);
loginUNPanel.add(loginPassword); // ***** adding it once *****
loginUNPanel.add(loginPTextField);
loginUNPanel.add(new JLabel(""));
loginUNPanel.add(login);
JPanel registerUNPanel = new JPanel(new GridLayout(4,2));
registerUNPanel.add(registerUsername);
registerUNPanel.add(registerUNTextField);
registerUNPanel.add(registerPassword);
registerUNPanel.add(registerPTextField);
registerUNPanel.add(registerEmail);
registerUNPanel.add(registerETextField);
registerUNPanel.add(new JLabel(""));
registerUNPanel.add(register);
loginUNPanel.add(loginPassword); // ***** adding it again. *****
JPanel main = new JPanel(new GridLayout(1,2));
Get rid of that 2nd addition:
// loginUNPanel.add(loginPassword); // *** this
JPanel main = new JPanel(new GridLayout(1,2));
I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}
There is no errors but when I Run it the content that I added into the JPanel won't appear, only the one not inside the JPanel appear.
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame
{
public static void main(String arg[])
{
SimpleGUI f = new SimpleGUI("GUI components");
f.setSize(600,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
SimpleGUI(String s)
{
setTitle(s);
setLayout(new GridLayout(3,2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel ("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel ("Enter age:");
JTextField tf2= new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold",true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5,20);
JList list = new JList(new Object[] {"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play",ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
}
}
I've updated your code. Have a look at this version:
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame {
public static void main(String arg[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SimpleGUI f = new SimpleGUI("GUI components");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public SimpleGUI(String s) {
setTitle(s);
setLayout(new GridLayout(3, 2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel("Enter age:");
JTextField tf2 = new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold", true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5, 20);
JList list = new JList(new Object[]{"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play", ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
/**
* Need to add the following lines
*/
this.add(p1);
this.add(p2);
this.add(p3);
this.pack();
this.setVisible(true);
}
}
A couple of pointers:
You need to add your components to your JFrame for them to actually show up.
Any updates to the user interface must happen on the event dispatch thread. Consequently you would notice that I've added a SwingUtilites.invokeLater() to the main. Have a look at this article to understand "Threading with Swing"
Where you you add your panels to the frame? Also, forgotten my java "rules and regulations": do you need to call "super()"?