Misaligned fields
What i want.
My code-
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Test2 extends JFrame {
JPanel panel = new JPanel();
JLabel label1 = new JLabel("Enter 1st Number");
JLabel label2 = new JLabel("Enter 2nd Number");
JLabel label3 = new JLabel("Press to add");
JLabel label4 = new JLabel("Check Answer");
JButton button = new JButton("Press");
JTextField text1 = new JTextField(50);
JTextField text2 = new JTextField(50);
public Test2() {
setTitle("Tutorial");
setVisible(true);
setSize(1080, 720);
setDefaultCloseOperation(EXIT_ON_CLOSE);
text1.setBounds(90,60,86,23);
text2.setBounds(233,60,92,23);
button.setBounds(161,109,89,23);
panel.add(text1);
panel.add(text2);
panel.add(button);
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
add(panel);
}
public static void main(String[] args) {
Test2 t = new Test2 ();
}
}
I am also going to write the code to add the 2 numbers and if a user enters letters, i am also gonna throw an exception. But i want them to align first. I can not use GUI form as this is for practice.
Don't use setBounds().
use Grid Layout of 4 rows and 2 columns
you need to use a Gridlayout with 2 columns and 4 rows to make them like that:
class Test2 extends JFrame {
JPanel panel = new JPanel();
JLabel label1 = new JLabel("Enter 1st Number");
JLabel label2 = new JLabel("Enter 2nd Number");
JLabel label3 = new JLabel("Press to add");
JLabel label4 = new JLabel("Check Answer");
JButton button = new JButton("Press");
JTextField text1 = new JTextField(50);
JTextField text2 = new JTextField(50);
public Test2() {
GridLayout gb = new GridLayout(4, 2);
panel.setLayout(gb);
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(button);
panel.add(label4);
add(panel);
setTitle("Tutorial");
setVisible(true);
setSize(250, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Test2 t = new Test2();
}
}
output:
Related
I have this code. When I run it, it displays a JFrame "menu" with two choices. When I click on a button it now displays the menu AND the new JFrame in the same window. And when I click on the other button, it adds with the existing things displayed on the same Window and adds and add inside. I'm thinking of using just one window/JFrame. How do I clear out the past JFrame? Is this possible?
package finals;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Finals extends JFrame implements ActionListener{
JLabel In1, In2, choice,menu;
JTextField In, Inn, choicee, info, menuu, lol,loll;
JButton PtoS, StoP,stop,ptos;
public Finals(){
setTitle("Menu");
setSize(200,150);
menu = new JLabel("Select a Program:");
PtoS = new JButton("Proposition to Sentence");
StoP = new JButton("Sentence to Proposition");
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
pane.add(menu);
pane.add(PtoS);
pane.add(StoP);
PtoS.addActionListener(this);
StoP.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == StoP){
setTitle("Sentence to Proposition");
setSize(500,500);
In1 = new JLabel("Enter First Sentence:", SwingConstants.RIGHT);
choice = new JLabel("Enter Operator:", SwingConstants.RIGHT);
In2 = new JLabel("Enter Second Sentence", SwingConstants.RIGHT);
lol = new JTextField("[a]= AND [b]= OR [c]= NOT");
lol.setEditable(false);
loll = new JTextField();
loll.setEditable(false);
In = new JTextField();
choicee = new JTextField();
Inn = new JTextField();
Container cont = getContentPane();
cont.setLayout(new GridLayout(5,5));
cont.add(In1);
cont.add(In);
cont.add(lol);
cont.add(loll);
cont.add(choice);
cont.add(choicee);
cont.add(In2);
cont.add(Inn);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
else if(e.getSource() == PtoS){
setTitle("Proposition to Sentence");
setSize(500,500);
In1 = new JLabel("Enter p:", SwingConstants.RIGHT);
choice = new JLabel("Enter Operator:", SwingConstants.RIGHT);
In2 = new JLabel("Enter q:", SwingConstants.RIGHT);
lol = new JTextField("[a]= AND [b]= OR [c]= NOT");
lol.setEditable(false);
loll = new JTextField();
loll.setEditable(false);
In = new JTextField();
choicee = new JTextField();
Inn = new JTextField();
Container cont = getContentPane();
cont.setLayout(new GridLayout(5,2));
cont.add(In1);
cont.add(In);
cont.add(lol);
cont.add(loll);
cont.add(choice);
cont.add(choicee);
cont.add(In2);
cont.add(Inn);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public static void main(String[] args){
Finals X = new Finals();
}
}
I am having trouble with the layout of my JFRAME
the layout is extremely difficult to me because i have just started learning it.
the layout I'm looking for is
select data START
DAY: "textField"
Month: "textField"
YEAR: "textField"
END DATE
DAY: "textField"
Month: "textField"
YEAR: "textField"
like it is in the end stage, why isn't top working
The below code shows the image above
import javax.swing.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;
public class TopUpHistoryScreen extends JDialog {
private JPanel mainPanel;
private JTextArea historyScreen;
public TopUpHistoryScreen()
{
setPanels();
setModalityType(ModalityType.APPLICATION_MODAL);
setSize(600, 600);
setVisible(true);
}
public void setPanels()
{
mainPanel = new JPanel(new GridLayout(0, 2));
JPanel containerPanel = new JPanel(new GridLayout(0, 1));
JPanel lowerPanel = new JPanel(new FlowLayout());
//JButton apply = new JButton("Select data area");
JButton exit = new JButton("Okay!");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
JButton checkDate = new JButton("check dates");
JLabel SelectData = new JLabel("Select data area\n");
JLabel START = new JLabel("START DATE!");
JLabel startDay = new JLabel("Day:");
JTextField sDay = new JTextField();
JLabel startMonth = new JLabel("Month:");
JTextField sMonth = new JTextField();
JLabel startYear = new JLabel("Year:");
JTextField sYear = new JTextField();
JLabel END = new JLabel("END DATE!");
JLabel endDay = new JLabel("Day:");
JTextField eDay = new JTextField();
JLabel endMonth = new JLabel("Month:");
JTextField eMonth = new JTextField();
JLabel endYear = new JLabel("Year:");
JTextField eYear = new JTextField();
JTextField Data = new JTextField();
JTextField touchOnTimeFieldminute = new JTextField();
historyScreen = new JTextArea(5,30);
JScrollPane scrolll = new JScrollPane(historyScreen);
mainPanel.add(SelectData);
mainPanel.add(START);
mainPanel.add(startDay);
mainPanel.add(sDay);
mainPanel.add(startMonth);
mainPanel.add(startYear);
mainPanel.add(sYear);
mainPanel.add(END);
mainPanel.add(endDay);
mainPanel.add(eDay);
mainPanel.add(endMonth);
mainPanel.add(eMonth);
mainPanel.add(endYear);
mainPanel.add(eYear);
mainPanel.add(checkDate);
//mainPanel.add(touchOnTimeFieldhour);
//mainPanel.add(SelectData);
//mainPanel.add(touchOnTimeFieldminute);
//mainPanel.add(touchOnTimem);
lowerPanel.add(scrolll);
lowerPanel.add(exit);
//lowerPanel.add(apply);
//touchOnTimeFieldhour.setSize(10,10);
containerPanel.add(mainPanel);
containerPanel.add(lowerPanel);
add(containerPanel);
}
}
change the mainPanel.add(component); sequence in setPanels() method to the following..
mainPanel.add(SelectData);
mainPanel.add(START);
mainPanel.add(startDay);
mainPanel.add(sDay);
mainPanel.add(startMonth);
mainPanel.add(sMonth);
mainPanel.add(startYear);
mainPanel.add(sYear);
mainPanel.add(END);
mainPanel.add(new JLabel());
mainPanel.add(endDay);
mainPanel.add(eDay);
mainPanel.add(endMonth);
mainPanel.add(eMonth);
mainPanel.add(endYear);
mainPanel.add(eYear);
mainPanel.add(checkDate);
and it will work..as you are using GridView to mainPanel, the elements will get the position according to the sequence you are adding it to mainPanel...
I have several JPanels that need to be displayed at the same time. They do display when I press the Port Settings button but I need there to be a title above each of the panels so the user will know which option they are selecting. My code as well as a screenshot is below.
package myGUI;
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(3000, 3000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JButton select1 = new JButton("Select");
JButton select2 = new JButton("Select");
JButton select3 = new JButton("Select");
JButton select4 = new JButton("Select");
//Make the drop down lists
String[] baudrates = {"57600", "115200", "128000", "256000"};
JComboBox baudlist = new JComboBox(baudrates);
String[] baudrates2 = {"57600", "115200", "128000", "256000"};
JComboBox baudlist2 = new JComboBox(baudrates2);
String[] bytesizes = {"7", "8"};
JComboBox bytelist = new JComboBox(bytesizes);
String[] bytesizes2 = {"7", "8"};
JComboBox bytelist2 = new JComboBox(bytesizes2);
String[] stopbit = {"1", "2"};
JComboBox stoplist = new JComboBox(stopbit);
String[] stopbit2 = {"1", "2"};
JComboBox stoplist2 = new JComboBox(stopbit2);
String[] flows = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist = new JComboBox(flows);
String[] flows2 = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist2 = new JComboBox(flows2);
String[] paritys = {"None", "Even", "Odd"};
JComboBox paritylist = new JComboBox(paritys);
String[] paritys2 = {"None", "Even", "Odd"};
JComboBox paritylist2 = new JComboBox(paritys2);
JLabel ipLabel = new JLabel("IP Address: ");
JLabel connectLabel = new JLabel("Connect Time: ");
JLabel sendLabel = new JLabel("Send Time Out: ");
JLabel receiveLabel = new JLabel("Receive Time Out: ");
JLabel portLabel = new JLabel("Port: ");
JLabel baudrate = new JLabel("Baud Rate: ");
JLabel bytesize = new JLabel("Byte Size: ");
JLabel stopbits = new JLabel("Stop Bits: ");
JLabel flow = new JLabel("Flow Con...: ");
JLabel parity = new JLabel("Parity: ");
JLabel stoLabel = new JLabel("Send Time Out: ");
JLabel rtoLabel = new JLabel("Receive Time Out: ");
JLabel portLabel2 = new JLabel("Port: ");
JLabel baudrate2 = new JLabel("Baud Rate: ");
JLabel bytesize2 = new JLabel("Byte Size: ");
JLabel stopbits2 = new JLabel("Stop Bits: ");
JLabel flow2 = new JLabel("Flow Con...: ");
JLabel parity2 = new JLabel("Parity: ");
JLabel stoLabel2 = new JLabel("Send Time Out: ");
JLabel rtoLabel2 = new JLabel("Receive Time Out: ");
JLabel portLabel3 = new JLabel("Port: ");
JLabel vendor = new JLabel("Vendor ID: ");
JLabel product = new JLabel("Product ID: ");
JLabel stoLabel3 = new JLabel("Send Time Out: ");
JLabel rtoLabel3 = new JLabel("Receive Time Out: ");
JLabel logLabel = new JLabel("Input / Output Log");
JTextField ip = new JTextField(10);
ip.setText("192.168.0.102");
JTextField ct = new JTextField(10);
ct.setText("5000");
JTextField rto = new JTextField(10);
rto.setText("5000");
JTextField sto = new JTextField(10);
sto.setText("5000");
JTextField port = new JTextField(10);
port.setText("12000");
JTextField sendto = new JTextField(10);
JTextField reto = new JTextField(10);
JTextField comport = new JTextField(10);
JTextField sendto2 = new JTextField(10);
JTextField reto2 = new JTextField(10);
JTextField comport2 = new JTextField(10);
JTextField vendorid = new JTextField(10);
JTextField productid = new JTextField(10);
JTextField sendtime = new JTextField(10);
JTextField receiveto = new JTextField(10);
JTextArea logbox = new JTextArea() {
#Override
public java.awt.Dimension getPreferredSize() {
return new Dimension(300, 450);
};
};
logLabel.setFont(new java.awt.Font("Tahoma", 3, 18));
logLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
logLabel.setText("Input / Output Log");
logbox.add(logLabel);
//Add components to the panels
final JPanel ethernetSettings = new JPanel();
ethernetSettings.setLayout(new GridLayout(6, 2));
ethernetSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
ethernetSettings.add(ipLabel);
ethernetSettings.add(ip);
ethernetSettings.add(connectLabel);
ethernetSettings.add(ct);
ethernetSettings.add(receiveLabel);
ethernetSettings.add(rto);
ethernetSettings.add(sendLabel);
ethernetSettings.add(sto);
ethernetSettings.add(portLabel);
ethernetSettings.add(port);
ethernetSettings.add(select1);
final JPanel usbHIDSettings = new JPanel();
usbHIDSettings.setLayout(new GridLayout(5, 2));
usbHIDSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbHIDSettings.add(vendor);
usbHIDSettings.add(vendorid);
usbHIDSettings.add(product);
usbHIDSettings.add(productid);
usbHIDSettings.add(stoLabel3);
usbHIDSettings.add(sendtime);
usbHIDSettings.add(rtoLabel3);
usbHIDSettings.add(receiveto);
usbHIDSettings.add(select2);
final JPanel usbCDCSettings = new JPanel();
usbCDCSettings.setLayout(new GridLayout(9, 2));
usbCDCSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbCDCSettings.add(baudrate2);
usbCDCSettings.add(baudlist);
usbCDCSettings.add(bytesize2);
usbCDCSettings.add(bytelist);
usbCDCSettings.add(stopbits2);
usbCDCSettings.add(stoplist);
usbCDCSettings.add(flow2);
usbCDCSettings.add(flowlist);
usbCDCSettings.add(parity2);
usbCDCSettings.add(paritylist);
usbCDCSettings.add(stoLabel2);
usbCDCSettings.add(sendto2);
usbCDCSettings.add(rtoLabel2);
usbCDCSettings.add(reto2);
usbCDCSettings.add(portLabel3);
usbCDCSettings.add(comport2);
usbCDCSettings.add(select3);
final JPanel rsSettings = new JPanel();
rsSettings.setLayout(new GridLayout(9, 2));
rsSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
rsSettings.add(baudrate);
rsSettings.add(baudlist2);
rsSettings.add(bytesize);
rsSettings.add(bytelist2);
rsSettings.add(stopbits);
rsSettings.add(stoplist2);
rsSettings.add(flow);
rsSettings.add(flowlist2);
rsSettings.add(parity);
rsSettings.add(paritylist2);
rsSettings.add(stoLabel);
rsSettings.add(sendto);
rsSettings.add(rtoLabel);
rsSettings.add(reto);
rsSettings.add(portLabel2);
rsSettings.add(comport);
rsSettings.add(select4);
final JPanel PortSettings = new JPanel();
PortSettings.setLayout(new GridLayout(1, 4));
PortSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
PortSettings.add(ethernetSettings);
PortSettings.add(rsSettings);
PortSettings.add(usbCDCSettings);
PortSettings.add(usbHIDSettings);
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog port = new JDialog(frame);
port.setTitle("Port Settings");
port.setSize(400, 400);
port.add(PortSettings);
port.pack();
port.setVisible(true);
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
account.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog accountDialog = new JDialog(frame);
accountDialog.setTitle("Account");
accountDialog.setSize(400, 400);
accountDialog.add(accountPanel);
accountDialog.pack();
accountDialog.setVisible(true);
}
});
JButton amount = new JButton("Amount");
amount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog amount2 = new JDialog(frame);
amount2.setTitle("Amount");
amount2.setSize(400, 400);
amount2.add(amountPanel);
amount2.pack();
amount2.setVisible(true);
}
});
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
approvordecl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog apprv = new JDialog(frame);
apprv.setTitle("Approve / Decline");
apprv.setSize(400, 400);
apprv.add(apprvordecl);
apprv.pack();
apprv.setVisible(true);
}
});
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
ButtonGroup approvegroup = new ButtonGroup();
approvegroup.add(apprve);
approvegroup.add(decline);
JPanel newButtonPanel = new JPanel();
newButtonPanel.add(online);
newButtonPanel.add(offline);
newButtonPanel.add(status);
newButtonPanel.add(reboot);
newButtonPanel.add(account);
newButtonPanel.add(amount);
newButtonPanel.add(reset);
newButtonPanel.add(approvordecl);
newButtonPanel.add(logLabel);
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.setLayout(new BorderLayout());
llpPanel.add(newButtonPanel, BorderLayout.PAGE_START);
llpPanel.add(logLabel, BorderLayout.CENTER);
llpPanel.add(new JScrollPane(logbox), BorderLayout.PAGE_END);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
For titles on JPanel, you can use Title Border as below.
TitledBorder title = BorderFactory.createTitledBorder("YOUR_TITLE");
YOURPANEL.setBorder(title);
You could try creating a border above each of the panels like this example:
Make a JPanel border with title like in Firefox
Try this:
myPanel.setBorder(BorderFactory.createTitledBorder("MyTitle"));
I think you want to set the title on the frame and not the panel. JPanels don't have titles, but their parent frames do.
Try this:
SwingUtilities.getRoot(yourPanel).setTitle("SomeTitle");
I'm trying to create (hand-coded) a GUI similair to the GUI shown below, however, only an empty frame shows.
Mock GUI:
I've used various layouts and SWING/AWT components to create the GUI and 4 JPanels which contain:
mainPanel: Contains all the panels in it.
listPanel: Contains the JTables, JLabels and the two JButtons
infoPanel: Contains the JLabels, JCheckBox and JTextBoxes.
addPanel: Contains the JLists and JButton
This is what I coded so far:
import java.awt.*;
import javax.swing.*;
import javax.swing.JTable;
public class GUI extends JFrame {
public void buildGui() {
JFrame frame = new JFrame("Hotel TV Scheduler");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(3,3));
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(2,2));
JPanel addPanel = new JPanel();
addPanel.setLayout(new FlowLayout());
mainPanel.add(listPanel, BorderLayout.LINE_START);
mainPanel.add(infoPanel, BorderLayout.LINE_END);
mainPanel.add(addPanel, BorderLayout.PAGE_END);
JTable chOneTable = new JTable();
JTable chTwoTable = new JTable();
JTable listTable = new JTable();
JLabel ch1Label = new JLabel("Channel 1");
JLabel ch2Label = new JLabel("Channel 2");
JLabel listLabel = new JLabel("List");
JButton rmvChOneButton = new JButton("Remove Channel");
JButton rmvChTwoButton = new JButton("Remove Channel");
listPanel.add(ch1Label);
listPanel.add(ch2Label);
listPanel.add(listLabel);
listPanel.add(chOneTable);
listPanel.add(chTwoTable);
listPanel.add(listTable);
listPanel.add(rmvChOneButton);
listPanel.add(rmvChTwoButton);
JLabel titleLabel = new JLabel("Title");
JLabel genreLabel = new JLabel("Genre");
JLabel durationLabel = new JLabel("Duration");
JLabel actorLabel = new JLabel("Actor");
JLabel directorLabel = new JLabel("Director");
JLabel rentableLabel = new JLabel("Rentable");
JLabel synLabel = new JLabel("Synopsis");
JTextField txtTitle = new JTextField();
JTextField txtGenre = new JTextField();
JTextField txtDuration = new JTextField();
JTextField txtActor = new JTextField();
JTextField txtDirector = new JTextField();
JTextField txtSynopsis = new JTextField();
JCheckBox rentCB = new JCheckBox();
infoPanel.add(titleLabel);
infoPanel.add(txtTitle);
infoPanel.add(genreLabel);
infoPanel.add(txtGenre);
infoPanel.add(durationLabel);
infoPanel.add(txtDuration);
infoPanel.add(actorLabel);
infoPanel.add(txtActor);
infoPanel.add(directorLabel);
infoPanel.add(txtDirector);
infoPanel.add(rentableLabel);
infoPanel.add(rentCB);
infoPanel.add(synLabel);
infoPanel.add(txtSynopsis);
JButton btnAddProg = new JButton("Add Program");
JList channelList = new JList();
JList timeList = new JList();
addPanel.add(btnAddProg);
addPanel.add(channelList);
addPanel.add(timeList);
frame.setVisible(true);
}
}
Anyone can tell me why only an empty frame is showing up ?
Thanks and Regards,
Brian
Yep just checked, you'll see something if you actually add the mainPanel to the frame, (looks nothing like the mock though!)
frame.setContentPane(mainPanel);
frame.pack();
You've not added mainPanel to the frame
MainClass(){
JFrame main = new JFrame("Login Form ");
main.setBounds(350,150,500,500);
main.setVisible(true);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name = new JTextField(10);
pass = new JTextField(10);
main.setLayout(new GridLayout(0,1));
JPanel pane = new JPanel();
main.add(pane);
main.add(new JLabel("Username: "));
pane.add(name);
//main.add(pane);
pane.add(new JLabel("Password: "));
pane.add(pass);
submit = new JButton("Submit");
pane.add(submit);
submit.addActionListener(new Handler());
}
I want to separate the text boxes in separate lines after the label username and name text box. I need to control the cursor to a new line.
i want to separate the text boxes in separate lines
import java.awt.*;
import javax.swing.*;
class MainClass {
JTextField name;
// This should be a JPasswordField!
JTextField pass;
JButton submit;
MainClass(){
JFrame main = new JFrame("Login Form ");
// Don't use this nonsense!
//main.setBounds(350,150,500,500);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name = new JTextField(10);
pass = new JTextField(10);
main.setLayout(new GridLayout(0,1));
JPanel pane = new JPanel(new GridLayout(0,1));
main.add(pane);
pane.add(new JLabel("Username: "));
pane.add(name);
pane.add(new JLabel("Password: "));
pane.add(pass);
submit = new JButton("Submit");
pane.add(submit);
//submit.addActionListener(new Handler());
main.pack();
main.setVisible(true);
}
public static void main(String[] args) {
MainClass mc = new MainClass();
}
}
If I was building a login screen, it might be laid out more along these lines (with the labels right justified and the button in it's own panel - left as an exercise for the reader).
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class MainClass {
JTextField name;
JPasswordField pass;
JButton submit;
MainClass(){
JFrame main = new JFrame("Login Form ");
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name = new JTextField(10);
pass = new JPasswordField(10);
JPanel gui = new JPanel(new BorderLayout(3,3));
gui.setBorder(new EmptyBorder(5,5,5,5));
main.setContentPane(gui);
JPanel labels = new JPanel(new GridLayout(0,1));
JPanel controls = new JPanel(new GridLayout(0,1));
gui.add(labels, BorderLayout.WEST);
gui.add(controls, BorderLayout.CENTER);
labels.add(new JLabel("Username: "));
controls.add(name);
labels.add(new JLabel("Password: "));
controls.add(pass);
submit = new JButton("Submit");
gui.add(submit, BorderLayout.SOUTH);
main.pack();
main.setVisible(true);
}
public static void main(String[] args) {
MainClass mc = new MainClass();
}
}
Use BoxLayout; create a JPanel, use SetLayout to set it to BoxLayout, and set the BoxLayout to PAGE_AXIS. Then things you add go one after another vertically down the 'page'. There are options for alignment, see the API for BoxLayout or the Oracle/Sun/Java tutorial on layout managers.