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);
}
Related
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've created a registration page in Java using different Layouts and I'm having trouble aligning the buttons on the same line and creating space between each JLabel.You can see it on the link (https://postimg.org/image/jaur8rxz7/).
This is the code that I have written
package liblog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Aregistration extends JFrame {
private JTextField text;
private JTextField text2;
private JPasswordField pass;
private JButton log1;
private JButton sign1;
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel();
JLabel label3 = new JLabel();
JLabel label2 = new JLabel();
Aregistration(){
super("Admin Registration");
setLayout(new FlowLayout(FlowLayout.CENTER,15,15));
setBounds(500,500,500,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(panel);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
label1=new JLabel("First Name");
label1.setToolTipText("Enter Your First Name");
text = new JTextField("Name",20);
label2=new JLabel("Registration No");
label2.setToolTipText("Enter Your Registation no");
text2 = new JTextField("Registation No",20);
label3 = new JLabel("Password");
label3.setToolTipText("Enter Your Password");
pass = new JPasswordField("Password",20);
log1 = new JButton("Log In");
sign1 = new JButton("Register");
panel.add(Box.createRigidArea(new Dimension(0, 0)));
panel.add(Box.createVerticalStrut(HEIGHT));
panel.add(label1);
panel.add(text);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(pass);
panel.add(log1);
panel.add(sign1);
setVisible(true);
}
public static void main(String[]args){new Aregistration();
}
}
I'm having trouble aligning the buttons on the same line
Create a second panel and add the buttons to the panel and then add this panel to your main panel:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));
buttonPanel.add( log1 );
buttonPanel.add( Box.createHorizontalStrut(5) );
buttonPanel.add( sign1 );
panel.add( buttonPanel );
and creating space between each JLabel
You can also use a vertical strut.
Read the section from the Swing tutorial on How to Use BoxLayout for more information and examples.
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);
I have container jpanel in which i used a boxlayout manager and what i do is i add another panels inside in which the added panel contains a label and textfield using flowlayout manager. everytime i add a panel inside it creates an annoying big space after another added panel. I want to reduce the spacing of the panels i have tried using setsize and setpreferredsize method to adjust it. Here is my code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500,312));
global.setSize(500,312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
//Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
global.add(new JSeparator());
//Name Field
JPanel c = new JPanel();
c.setSize(100, 1);
c.setPreferredSize(new Dimension(100,1));
c.setLayout(new FlowLayout());
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20,25));
c.add(label);
c.add(text);
global.add(c);
//Hostname Field
JPanel c1 = new JPanel();
c1.setSize(100, 1);
c1.setPreferredSize(new Dimension(100,1));
c1.setLayout(new FlowLayout());
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20,25));
c1.add(label2);
c1.add(text1);
global.add(c1);
BoxLayout is a pretty aggressive LayoutManager and doesn't always honour the preferred size of components within it. Instead, we must set the maximum size of BoxLayout components to prevent them from being stretched. Additionally, we need to add a Box via Box.createVerticalGlue() - this is special component that gets stretched (rather than the other components).
Here is the rewritten code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500, 312));
global.setSize(500, 312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
// Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
JSeparator sep = new JSeparator();
sep.setMaximumSize(new Dimension((int) sep.getMaximumSize().getWidth(), 50));
global.add(sep);
// Name Field
JPanel c = new JPanel();
c.setMaximumSize(new Dimension((int) c.getMaximumSize().getWidth(), 50));
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20, 25));
c.add(label);
c.add(text);
global.add(c);
// Hostname Field
JPanel c1 = new JPanel();
c1.setMaximumSize(new Dimension((int) c1.getMaximumSize().getWidth(), 50));
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20, 25));
c1.add(label2);
c1.add(text1);
global.add(c1);
global.add(Box.createVerticalGlue());
How can I get rid of that gray box?
This is what I'm talking about:
i would really appreciate if you could help me out
Full code here: http://pastebin.com/nrpCTjvV
public final void initUI() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(new Insets(90, 155, 40, 60)));
JButton NewGame = new JButton ("New Game!");
JButton Highscore = new JButton("Highscore");
JButton Credits = new JButton ("Credits");
JButton Website = new JButton ("Website");
JButton Exit = new JButton ("Exit");
panel.add(NewGame);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Highscore);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Credits);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Website);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Exit);
final ButtonGroup entreeGroup = new ButtonGroup();
JRadioButton radioButton;
panel.add(radioButton = new JRadioButton("Music1"));
radioButton.setActionCommand("Music1");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("Music2"));
radioButton.setActionCommand("Music2");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("No Music", true));
radioButton.setActionCommand("No Music");
entreeGroup.add(radioButton);
add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setSize(400, 400);
}
add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
The default layout manager for a JFrame is a BorderLayout. When you add a component without specifying a constraint the component is added to the CENTER. You can't add multiple components to a single location.
Instead you need to use a different component as the background. Then you add your panel to this component. Check out Background Panel. Then code would be something like:
Background background = new BackgroundPanel(...);
background.add(panel);
add(background);
setResizable(false);
pack();
...