I'm pretty new to java. And i can't get this to work... I'm trying to add components using this code:
public class Board
{
public static void main(String[] args) {
JFrame window = new JFrame("Tellraw Generator");
window.setVisible(true);
window.setSize(400, 600);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel();
panel.setLayout(null);
window.add(panel);
//"Generate" Button
JButton button1 = new JButton("Generate");
button1.setBounds(262, 485, 100, 37);
panel.add(button1);
//"Add Text" Button
JButton button2 = new JButton("Add Text");
button2.setBounds(51, 337, 88, 33);
panel.add(button2);
//Title
JLabel txt1 = new JLabel("Tellraw Generator");
txt1.setFont(new Font("Minecrafter Alt Regular", Font.BOLD, 29));
txt1.setBounds(61, 18, 278, 30);
panel.add(txt1);
}
}
But when i'm trying to do it, the components aren't showing up on the screen.
So are there someone who can tell me why it isn't working/Showing up and how i can add it in ?
Thanks
You mean that JLabel and JButton don't appear? right ? not JTextField because there is no JTextField in the code
Anyway just add this line of code to the end:
window.add(panel);
Here you are adding the JPanel that contains all the JComponents to the JFrame
and always: have the setVisible(true) call as last call. Everything that you add to the window/frame must be done before the setVisible call
This is how to declare a JTextField
final JTextField variableName = new JTextField(size);
This is how you get the text from JTextField
variableName.getText()
Hope that helps.
Try this:
public class Board
{
public static void main(String[] args) {
JFrame window = new JFrame("Tellraw Generator");
window.setVisible(true);
window.setSize(400, 600);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel();
panel.setLayout(null);
window.add(panel);
//"Generate" Button
JButton button1 = new JButton("Generate");
button1.setBounds(262, 485, 100, 37);
panel.add(button1);
//"Add Text" Button
JButton button2 = new JButton("Add Text");
button2.setBounds(51, 337, 88, 33);
panel.add(button2);
//Title
JTextField txt1 = new JTextField ();
txt1.setFont(new Font("Minecrafter Alt Regular", Font.BOLD, 29));
txt1.setBounds(61, 18, 278, 30);
panel.add(txt1);
}
}
Related
Hello Everyone I am new to Java so I am sure I am doing something obviously wrong here but I just keep going in circles.
I am trying to add a scroll to a JTextarea. Here is what I have tried but it doesn't show up.
textarea1 = new JTextArea();
textarea1.setBounds(251,26,795,345);
textarea1.setBackground(new Color(255,255,255));
textarea1.setForeground(new Color(0,0,0));
textarea1.setEnabled(true);
textarea1.setFont(new Font("sansserif",0,12));
textarea1.setText("");
textarea1.setBorder(BorderFactory.createBevelBorder(1));
textarea1.setVisible(true);
JScrollPane ScrollPane1 = new JScrollPane(textarea1);
ScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//adding components to contentPane panel
contentPane.add(browseFileOne);
contentPane.add(browseOutput);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button4);
contentPane.add(fileOneText);
contentPane.add(fileTwoText);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(outputTextFile);
contentPane.add(textarea1);
see working example:
https://repl.it/repls/DimpledDefensiveSourcecode
Code:
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
public Main() {
this.setSize(400, 400);
this.setLocation(0, 0);
this.setResizable(false);
this.setTitle("Application");
JPanel painel = new JPanel(null);
// Creating the Input
JTextField tf1 = new JTextField("Some random text", 15);
tf1.setBounds(5, 5, this.getWidth() - 120, 20);
tf1.setColumns(10);
tf1.setText("Omg");
// resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
painel.add(tf1);
// Creating the button
JButton button1 = new JButton("Send");
button1.setBounds(290, 5, 100, 19);
painel.add(button1);
// Creating the TextArea
JTextArea ta1 = new JTextArea(15, 20);
JScrollPane scr = new JScrollPane(ta1,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane
ta1.setBounds(5, 35, 385, 330);
ta1.setLineWrap(true);
ta1.setWrapStyleWord(true);
scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout
painel.add(scr);// Add you scroll pane to container
this.add(painel);
this.setVisible(true);
}
}
When the JComboBox, cmbox was not added to a JPanel, two panels, p1 & p2 could be rendered. You may comment out the combo box portion to see the result. But after I added the combo box into one of the panels, all panels were not rendered.
My code is like the following:
import java.awt.*;
import javax.swing.*;
public class TestCombo {
public static void main(String[] args) {
JFrame frame = new JFrame("康樂彩歌");
frame.setVisible(true);
frame.setBounds(0, 0, 1368, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.CYAN);
JRadioButton rb1 = new JRadioButton("加簡譜", false);
rb1.setFont(new Font("新細明體", Font.PLAIN, 20));
JRadioButton rb2 = new JRadioButton("加人聲", false);
rb2.setFont(new Font("新細明體", Font.PLAIN, 20));
rb1.setBounds(450, 180, 50, 50);
rb2.setBounds(500, 180, 50, 50);
JButton btPlay = new JButton("PLAY");
btPlay.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
btPlay.setBounds(100, 20, 100, 20);//x axis, y axis, width, height
JButton btStop = new JButton("STOP");
btStop.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
btStop.setBounds(140, 20, 100, 20);//x axis, y axis, width, height
//p1.add(cmbox);
p1.add(rb1);
p1.add(rb2);
p1.add(btPlay);
p1.add(btStop);
p1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setBorder(BorderFactory.createLineBorder(Color.red));
JComboBox cmbox = new JComboBox(); //The JComboBox to be added to a JPanel
cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));
cmbox.addItem("紫竹調");
cmbox.addItem("走一同去郊遊");
cmbox.addItem("大野狼");
cmbox.addItem("歸來吧蘇連多");
cmbox.addItem("追尋");
cmbox.addItem("三輪車");
cmbox.addItem("我家門前有小河");
cmbox.addItem("漁家樂");
cmbox.addItem("嚕啦啦");
cmbox.addItem("踏雪尋梅");
p2.add(cmbox);
frame.add(p1, BorderLayout.PAGE_START);
frame.add(p2, BorderLayout.CENTER);
}
}
Implementing the changes as detailed below the code, solves the problem.
import java.awt.*;
import javax.swing.*;
public class TestCombo {
public static void main(String[] args) {
JFrame frame = new JFrame("康樂彩歌");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.CYAN);
JRadioButton rb1 = new JRadioButton("加簡譜", false);
rb1.setFont(new Font("新細明體", Font.PLAIN, 20));
JRadioButton rb2 = new JRadioButton("加人聲", false);
rb2.setFont(new Font("新細明體", Font.PLAIN, 20));
JButton btPlay = new JButton("PLAY");
btPlay.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
JButton btStop = new JButton("STOP");
btStop.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
p1.add(rb1);
p1.add(rb2);
p1.add(btPlay);
p1.add(btStop);
p1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setBorder(BorderFactory.createLineBorder(Color.red));
JComboBox cmbox = new JComboBox(); //The JComboBox to be added to a JPanel
cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));
cmbox.addItem("紫竹調");
cmbox.addItem("走一同去郊遊");
cmbox.addItem("大野狼");
cmbox.addItem("歸來吧蘇連多");
cmbox.addItem("追尋");
cmbox.addItem("三輪車");
cmbox.addItem("我家門前有小河");
cmbox.addItem("漁家樂");
cmbox.addItem("嚕啦啦");
cmbox.addItem("踏雪尋梅");
p2.add(cmbox);
frame.add(p1, BorderLayout.PAGE_START);
frame.add(p2, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
btPlay.setBounds(100, 20, 100, 20); Do not set the bounds of components. Let the layouts (padding and borders) do their job.
frame.setBounds(0, 0, 1368, 500); That's just a guess, and if it's the right guess on one OS, it will be the wrong guess on others. Instead pack() the window after components are added.
Sidebar: GUIs should be started on the EDT. (Not implemented above: 'batteries not included'.)
I'm trying to add a JScrollPane to JTextArea, but when I add it, JPane is not showing my textarea anymore. Without JScrollPane it shows it, but then I can't display all the information retrieved from a file.
This is my code for one of textAreas which I want to be wrapped with JScrollPane.
public GUI_CWK()
{
//frame details
setSize(450,400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("H.o. | Location Database");
setExtendedState(JFrame.MAXIMIZED_BOTH);
getContentPane().setBackground(Color.YELLOW);
// Useing Lable to set a title for multiple buttons
lbl2 = new JLabel();
lbl2.setSize(420,85);
lbl2.setLocation(15,0);
lbl2.setEnabled(false);
lbl2.setFont(new Font ("arial",5,17));
lbl2.setText("Options to Manipulate with Data");
add(lbl2);
// Adding buttons to JFrame
btn1 = new JButton("Add new entry");
btn1.setSize(120,20);
btn1.setLocation(7,80);
btn1.addActionListener(this);
add(btn1);
btn2 = new JButton("Search");
btn2.setSize(120,20);
btn2.setLocation(7,110);
btn2.addActionListener(this);
add(btn2);
btn3 = new JButton("Update Entry");
btn3.setSize(120,20);
btn3.setLocation(150,110);
btn3.addActionListener(this);
add(btn3);
btn4 = new JButton("Print All");
btn4.setSize(120,20);
btn4.setLocation(150,80);
btn4.addActionListener(this);
add(btn4);
btn5 = new JButton("Print Arrays");
btn5.setSize(120,20);
btn5.setLocation(7,140);
btn5.addActionListener(this);
add(btn5);
btn6 = new JButton("Delete Entry");
btn6.setSize(120,20);
btn6.setLocation(300,80);
btn6.addActionListener(this);
add(btn6);
btn7 = new JButton("Delete ALL");
btn7.setSize(120,20);
btn7.setLocation(300,110);
btn7.addActionListener(this);
add(btn7);
lbl1 = new JLabel();
lbl1.setSize(420,85);
lbl1.setLocation(15,190);
lbl1.setEnabled(false);
lbl1.setFont(new Font ("arial",5,17));
lbl1.setText("Extra Options");
add(lbl1);
btn8 = new JButton("Sort Data");
btn8.setSize(160,20);
btn8.setLocation(7,265);
btn8.addActionListener(this);
add(btn8);
btn9 = new JButton("Open Text file");
btn9.setSize(160,20);
btn9.setLocation(7,300);
btn9.addActionListener(this);
add(btn9);
btn11 = new JButton("Retrieve from text file");
btn11.setSize(160,20);
btn11.setLocation(240,265);
btn11.addActionListener(this);
add(btn11);
btn12 = new JButton("Exit Program");
btn12.setSize(160,20);
btn12.setLocation(240,300);
btn12.addActionListener(this);
add(btn12);
lbl3 = new JLabel();
lbl3.setSize(420,85);
lbl3.setLocation(15,340);
lbl3.setEnabled(false);
lbl3.setFont(new Font ("arial",5,17));
lbl3.setText("Terminal Window");
add(lbl3);
// Adding textArea to display each user entered value
textArea2 = new JTextArea(5, 20);
textArea2.setEditable(false);
textArea2.setSize(440,300);
textArea2.setLocation(7,400);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial",5,14));
sp2 = new JScrollPane(textArea2);
sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp2);
lbl4 = new JLabel();
lbl4.setSize(420,40);
lbl4.setLocation(570,10);
lbl4.setEnabled(false);
lbl4.setFont(new Font ("arial",5,17));
lbl4.setText("Data Display");
add(lbl4);
textArea = new JTextArea(55, 50);
textArea.setEditable(false);
textArea.setSize(800,640);
textArea.setLocation(560,60);
textArea.setFont(new Font("arial",5,19));
textArea.setDisabledTextColor(Color.black);
sp = new JScrollPane(textArea);
sp.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp);
setVisible(true);
}
I think that this should solve your problem.
You'll notice that I erased the thiss between the addActionListener() methods because you have to specify what do you want the ActionListeners to do.
StartingPoint (main) class:
public class StartingPoint{
public static void main(String[] args){
GUI_CWK gui_cwk = new GUI_CWK(); //Creates a new instance of "GUI_CWK".
gui_cwk.setVisible(true); //You set the gui_cwk to visible.
}
}
GUI_CWK class:
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class GUI_CWK extends JFrame{
private static final long serialVersionUID = 1L;
public GUI_CWK() {
// frame details
this.setSize(450, 400);
JPanel panel = new JPanel();
this.setLayout(null); /* Each time that you want to refer to the classyou are in, you say "this" */
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("H.o. | Location Database");
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.getContentPane().setBackground(Color.YELLOW);
// Using labels to set a title for multiple buttons
JLabel lbl2 = new JLabel(); /*Remember that you have to create the instances of an object by naming the class before the name of the instance */
lbl2.setSize(420, 85);
lbl2.setLocation(15, 0);
lbl2.setEnabled(false);
lbl2.setFont(new Font("arial", 5, 17));
lbl2.setText("Options to Manipulate with Data");
panel.add(lbl2);
// Adding buttons to JFrame
JButton btn1 = new JButton("Add new entry");
btn1.setSize(120, 20);
btn1.setLocation(7, 80);
btn1.addActionListener(/* Insert ActionListener */);
panel.add(btn1); //The button is added to the panel, so then the last one can be added to the JFrame at the end.
JButton btn2 = new JButton("Search");
btn2.setSize(120, 20);
btn2.setLocation(7, 110);
btn2.addActionListener(/* Insert ActionListener */);
panel.add(btn2);
JButton btn3 = new JButton("Update Entry");
btn3.setSize(120, 20);
btn3.setLocation(150, 110);
btn3.addActionListener(/* Insert ActionListener */);
panel.add(btn3);
JButton btn4 = new JButton("Print All");
btn4.setSize(120, 20);
btn4.setLocation(150, 80);
btn4.addActionListener(/* Insert ActionListener */);
panel.add(btn4);
JButton btn5 = new JButton("Print Arrays");
btn5.setSize(120, 20);
btn5.setLocation(7, 140);
btn5.addActionListener(/* Insert ActionListener */);
panel.add(btn5);
JButton btn6 = new JButton("Delete Entry");
btn6.setSize(120, 20);
btn6.setLocation(300, 80);
btn6.addActionListener(/* Insert ActionListener */);
panel.add(btn6);
JButton btn7 = new JButton("Delete ALL");
btn7.setSize(120, 20);
btn7.setLocation(300, 110);
btn7.addActionListener(/* Insert ActionListener */);
panel.add(btn7);
JLabel lbl1 = new JLabel();
lbl1.setSize(420, 85);
lbl1.setLocation(15, 190);
lbl1.setEnabled(false);
lbl1.setFont(new Font("arial", 5, 17));
lbl1.setText("Extra Options");
panel.add(lbl1);
JButton btn8 = new JButton("Sort Data");
btn8.setSize(160, 20);
btn8.setLocation(7, 265);
btn8.addActionListener(/* Insert ActionListener */);
panel.add(btn8);
JButton btn9 = new JButton("Open Text file");
btn9.setSize(160, 20);
btn9.setLocation(7, 300);
btn9.addActionListener(/* Insert ActionListener */);
panel.add(btn9);
JButton btn11 = new JButton("Retrieve from text file");
btn11.setSize(160, 20);
btn11.setLocation(240, 265);
btn11.addActionListener(/* Insert ActionListener */);
panel.add(btn11);
JButton btn12 = new JButton("Exit Program");
btn12.setSize(160, 20);
btn12.setLocation(240, 300);
btn12.addActionListener(/* Insert ActionListener */);
panel.add(btn12);
JLabel lbl3 = new JLabel();
lbl3.setSize(420, 85);
lbl3.setLocation(15, 340);
lbl3.setEnabled(false);
lbl3.setFont(new Font("arial", 5, 17));
lbl3.setText("Terminal Window");
panel.add(lbl3);
// Adding textArea to display each user entered value
JTextArea textArea2 = new JTextArea(5, 20);
textArea2.setEditable(false);
textArea2.setSize(440, 300);
textArea2.setLocation(7, 400);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial", 5, 14));
JScrollPane sp2 = new JScrollPane(textArea2);
sp2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea2.add(sp2); //Here you add the scrollPanel to the textArea,
//in the original code you were adding it to the frame
panel.add(textArea2);
JLabel lbl4 = new JLabel();
lbl4.setSize(420, 40);
lbl4.setLocation(570, 10);
lbl4.setEnabled(false);
lbl4.setFont(new Font("arial", 5, 17));
lbl4.setText("Data Display");
panel.add(lbl4);
JTextArea textArea = new JTextArea(55, 50);
textArea.setEditable(false);
textArea.setSize(800, 640);
textArea.setLocation(560, 60);
textArea.setFont(new Font("arial", 5, 19));
textArea.setDisabledTextColor(Color.black);
JScrollPane sp1 = new JScrollPane(textArea);
sp1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setVisible(true);
textArea.add(sp1);
panel.add(textArea);
this.add(panel); //Everything which was added to the panel now is added to the frame.
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Here is an example of using a scroll pane.
import javax.swing.*;
import java.awt.*;
public class ScrollPaneExample{
public void buildGui(){
JFrame frame = new JFrame("button and scrollpane");
JButton button = new JButton("example");
JTextArea area = new JTextArea(20, 20);
//this is where we will place our components.
//It has a flow layout by default.
JPanel content = new JPanel();
JScrollPane sp = new JScrollPane(area);
content.add(button);
content.add(sp);
//this line limits the size of the scroll pane.
sp.setPreferredSize(new Dimension(400, 60));
//The frame will use our panel instead of the default one.
frame.setContentPane(content);
//sizes the frame to the content area.
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args){
EventQueue.invokeLater(()->{new ScrollPaneExample().buildGui();});
}
}
The main difference is that I set my content pane so I know its layout manager. You appear to be manually setting all of the sizes, which I am surprised it works. You should choose a good layout manager. pick one you think does what you want.
Also note that this is a minimal example that will compile.
Thank you for help guys, I found out the answer. As #matt told me, I should add size and location to JScrollPane instead of textArea. So I did it and it works for me now.
code is below.
textArea2 = new JTextArea(5,20);
textArea2.setEditable(false);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial",5,14));
sp2 = new JScrollPane(textArea2);
sp2.setSize(440,300);
sp2.setLocation(7,400);
sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp2);
so im making a program for my project.
and when i clicked a button it must open anohter frame and make the button unclickable. and when you closed the popup frame the button must re enable. so this is
my main frame
package Option2;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainMenu {
int intCtr1 = 0;
JFrame frame1 = new JFrame("EXD LAN PARTY");
JButton Button1 = new JButton();
JButton Button2 = new JButton();
JButton Button3 = new JButton();
JButton Button4 = new JButton();
JLabel Label1 = new JLabel();
public void MainMenu(){
//BUTTON1
Button1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI2.jpg")));
Button1.setBackground(Color.white);
Button1.setBounds(50, 350, 150, 150);
Button1.setToolTipText("Personal Info");
//BUTTON1 END
//BUTTON2
Button2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PC.jpg")));
Button2.setBackground(Color.white);
Button2.setBounds(250, 350, 150, 150);
Button2.setToolTipText("PC INFO");
//BUTTON2 END
//BUTTON3
Button3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Games.jpg")));
Button3.setBackground(Color.white);
Button3.setBounds(450, 350, 150, 150);
Button3.setToolTipText("Games");
//BUTTON3 END
//BUTTON4 END
Button4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Players.jpg")));
Button4.setBackground(Color.white);
Button4.setBounds(650, 350, 150, 150);
Button3.setToolTipText("Players");
//BUTTON4 END
//LABEL1
Label1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/EXD.jpg")));
Label1.setBounds(50, 50, 800, 250);
//LABEL1 END
//Frame1
frame1.getContentPane().setBackground(Color.black);
frame1.setResizable(false);
frame1.setLayout(null);
frame1.setSize(870,650);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(Label1);
frame1.add(Button1);
frame1.add(Button2);
frame1.add(Button3);
frame1.add(Button4);
//Frame1 END
Button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PersonalInfo objPI = new PersonalInfo();
objPI.Menu1();
Button1.setEnabled(false);
}
});
Button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PCInfo objPCI = new PCInfo();
objPCI.Menu2();
}
});
Button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Games objGames = new Games();
objGames.Menu3();
}
});
Button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Players objPlayers = new Players();
objPlayers.Menu4();
}
});
}
public void dim1(){
if(intCtr1 == 1){
MainMenu objMM = new MainMenu();
objMM.Button1.setEnabled(true);
System.out.println("SD");
}
}
}
**and this is my sub frame**
package Option2;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PersonalInfo {
String[] arrSex = {"Male","Female"};
JFrame frame1 = new JFrame("Personal Info");
JLabel label1 = new JLabel("ID");
JLabel label2 = new JLabel("Last Name");
JLabel label3 = new JLabel("First Name");
JLabel label4 = new JLabel("Middle Name");
JLabel label5 = new JLabel("SEX");
JLabel label6 = new JLabel();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JTextField tf4 = new JTextField();
JComboBox CB1 = new JComboBox(arrSex);
JButton Button1 = new JButton("NEW");
JButton Button2 = new JButton("SAVE");
JButton Button3 = new JButton("EDIT");
JButton Button4 = new JButton("CANCEL");
JButton Button5 = new JButton();
JButton Button6 = new JButton();
JButton Button7 = new JButton();
JButton Button8 = new JButton();
public void Menu1(){
//Frame1
frame1.add(label6);
frame1.add(label1);
frame1.add(tf1);
frame1.add(label2);
frame1.add(tf2);
frame1.add(label3);
frame1.add(tf3);
frame1.add(label4);
frame1.add(tf4);
frame1.add(label5);
frame1.add(CB1);
frame1.add(Button1);
frame1.add(Button2);
frame1.add(Button3);
frame1.add(Button4);
frame1.add(Button5);
frame1.add(Button6);
frame1.add(Button7);
frame1.add(Button8);
frame1.setVisible(true);
frame1.getContentPane().setBackground(Color.black);
frame1.setSize(600,600);
frame1.setResizable(false);
frame1.setLayout(null);
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
MainMenu objMM = new MainMenu();
objMM.intCtr1=1;
objMM.dim1();
}
});
//Frame1 End
//LABEL6
label6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/PI.jpg")));
label6.setBounds(30, 5, 800, 250);
//LABEL6 END
//LABEl1
label1.setBounds(100, 220, 50,50);
label1.setForeground(Color.white);
label1.setFont(new Font("Serif", Font.BOLD, 18));
//Label1 end
//Tf
tf1.setBounds(130, 230, 400,30);
tf1.setEnabled(false);
SmartC objSMC = new SmartC();
tf1.setText(objSMC.SmartCounter("ABC123415XYZS"));
//tf end
//label2
label2.setBounds(35, 255, 120,50);
label2.setForeground(Color.white);
label2.setFont(new Font("Serif", Font.BOLD, 18));
//label2 end
//Tf2
tf2.setBounds(130, 270, 400,30);
//tf2 end
//label3
label3.setBounds(35, 295, 120,50);
label3.setForeground(Color.white);
label3.setFont(new Font("Serif", Font.BOLD, 18));
//label3 end
//Tf3
tf3.setBounds(130, 310 , 400, 30);
//tf3 end
//label4
label4.setBounds(15, 335, 120,50);
label4.setForeground(Color.white);
label4.setFont(new Font("Serif", Font.BOLD, 18));
//label4 end
//Tf4
tf4.setBounds(130, 350 , 400, 30);
//tf4 end
//label4
label5.setBounds(85, 375, 120,50);
label5.setForeground(Color.white);
label5.setFont(new Font("Serif", Font.BOLD, 18));
//label4 end
//cb1
CB1.setBounds(130, 390, 100, 30);
CB1.setBackground(Color.white);
//cb1 end
//button1
Button1.setBounds(35, 450, 100, 30);
Button1.setBackground(Color.white);
//
//
Button2.setBounds(150, 450, 100, 30);
Button2.setBackground(Color.white);
//
//
Button3.setBounds(335, 450, 100, 30);
Button3.setBackground(Color.white);
//
//
Button4.setBounds(450, 450, 100, 30);
Button4.setBackground(Color.white);
//
//
Button5.setBounds(35, 500, 100, 50);
Button5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/First.jpg")));
Button5.setBackground(Color.white);
//
//
Button6.setBounds(150, 500, 100, 50);
Button6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Previous.jpg")));
Button6.setBackground(Color.white);
//
//
Button7.setBounds(335, 500, 100, 50);
Button7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Next.jpg")));
Button7.setBackground(Color.white);
//
//
Button8.setBounds(450, 500, 100, 50);
Button8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Option2/Last.jpg")));
Button8.setBackground(Color.white);
//
//
}
}
Any Suggestions about my coding is accepted. Sorry Still learning about Java :)
and when i clicked a button it must open anohter frame
You should NOT be creating another JFrame.
Instead you should be creating a modal JDialog. The dialog will not allow you to click on the frame until the dialog is closed.
Any Suggestions about my coding is accepted
Follow Java naming conventions. Variable names should NOT start with an upper case character. Sometimes you follow this guideline and sometimes you don't. Be consistent!
Don't use setBounds(...). Swing was designed to be used with layout managers!
First, please read Java naming conventions.
You need to pass the reference of MainMenu to PersonalInfo in order to achieve what you need, here:
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
PersonalInfo objPI = new PersonalInfo(this);
objPI.menu1();
button1.setEnabled(false);
}
});
And you need to add a constructor to PersonalInfo:
private MainMenu m;
public PersonalInfo(MainMenu m) {
this.m = m;
}
Add a public method to MainMenu:
public void enableMyButton() {
button1.setEnabled(true);
}
Now you can add an event listener to PersonalInfo to enable the button of the MainMenu frame:
frame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
this.m.enableMyButton();
}
});
My JLabel and JTextField are not appearing overtop the image and I'm not sure why. I've put the image on a JLabel and setOpaque(false) but no luck. Any advice? Thanks for the help in advance.
private JTextField tf;
private JLabel jl2;
private JLabel jl3;
public void window() {
ImageIcon ic = new ImageIcon("hangman.png");
JFrame gameFrame = new JFrame();
JPanel jp = new JPanel();
jp.setOpaque(false); //!!
jp.setBorder(BorderFactory.createTitledBorder(""));
JLabel img = new JLabel(ic, JLabel.CENTER);
img.setOpaque(false);
JLabel jl = new JLabel("Enter a Letter:");
jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
tf = new JTextField(1);
jl2 = new JLabel("Letters Used: ");
jl3 = new JLabel();//blank spaces
tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
jl2.setFont(new Font("Rockwell", Font.PLAIN, 20));
jp.add(jl);
jp.add(tf);
jp.add(jl2);
jp.add(jl3);
gameFrame.add(img);
img.add(jp);
gameFrame.setTitle("Hangman");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setIconImage(
new ImageIcon("Hangman-Game-grey.png").getImage());
gameFrame.setResizable(false);
gameFrame.pack();
gameFrame.setLocationRelativeTo(null);
gameFrame.setVisible(true);
A JLabel doesn't use a layout manager by default so components added to the label will not be painted.
Try setting the layout manager. Maybe:
img.setLayout( new BorderLayout() );
or choose a layout that meets your requirements.
You should add image, label and textfield objecs to panel, respectively. Then, you should add that panel to your frame. Also, put gameFrame.add(img); code before calling jp.add() methods. I don't know, if this is what you want but from what you try to do, I can say below code should work:
public void window()
{
ImageIcon ic = new ImageIcon("hangman.png");
JFrame gameFrame = new JFrame();
JPanel jp = new JPanel();
jp.setOpaque(false); // !!
jp.setBorder(BorderFactory.createTitledBorder(""));
JLabel img = new JLabel(ic, JLabel.CENTER);
img.setOpaque(false);
JLabel jl = new JLabel("Enter a Letter:");
jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
tf = new JTextField(1);
jl2 = new JLabel("Letters Used: ");
jl3 = new JLabel();// blank spaces
tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
jl2.setFont(new Font("Rockwell", Font.PLAIN, 20));
jp.add(img);
jp.add(jl);
jp.add(tf);
jp.add(jl2);
jp.add(jl3);
gameFrame.add(jp);
gameFrame.setTitle("Hangman");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setIconImage(new ImageIcon("1.jpg").getImage());
gameFrame.setResizable(false);
gameFrame.pack();
gameFrame.setLocationRelativeTo(null);
gameFrame.setVisible(true);
}