I am learning java GUI programming and I would like to create button. Problem is when button is created it is huge. How I can resize button?
JFrame frame = new JFrame("Ikkuna <3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel teksti = new JLabel("assdad", SwingConstants.CENTER);
teksti.setFont(new Font("Eras Demi ITC", Font.PLAIN, 32));
teksti.setForeground(Color.BLUE);//tekstin väri
ImageIcon img = new ImageIcon("C:/Users/account/Documents/NetBeansProjects/eka/src/eka/Kuvat/Trollface.PNG"); //Tämä on kuvake(icon).
frame.setIconImage(img.getImage());
JButton nappula = new JButton("Start");
frame.getContentPane().add(nappula);
nappula.setPreferredSize(new Dimension(83, 291));
nappula.setLocation(500, 350);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
teksti.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(teksti, BorderLayout.PAGE_START);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
I also tried this:
nappula.setPreferredSize(new Dimension(83, 291));
You Can set the fonts to the button and change its size
Font f = new Font("Times New Roman",Font.BOLD,20);
nappula.setFont(f);
Set it of any size change 20 to any value......
Try it....
You're using default layout from the looks of it. Try using
nappula.setSize(new Dimension(x, y));
Related
I am using Netbeans to create a Java desktop application. I have created two different JPanels. In one I have inserted a button, and in the other just some settings. How can I connect the JButton, in the other JPanel, to change the settings of the other one? What code should I use? (Keep in mind that I am a beginner.)
A very simple programm. It dosen't matter if the button and the label are in the same JPanel or not.
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 300, 300);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 150));
JLabel label = new JLabel("Test string");
JButton button = new JButton("Push me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
label.setText("change text");
}
});
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
I cannot display an image. I have tried both ImageIO and ImageIcon.
This is my code:
public TestGUImain()
{
JFrame frame = new JFrame("Basic Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
JLabel lblText = new JLabel("Hello World", JLabel.CENTER);
lblText.setBounds(10, 10, 100, 200);
panel.add(lblText);
ImageIcon image = new ImageIcon(getClass().getResource("150_leading_side_prep.jpg"));
JLabel lblImage = new JLabel(image, JLabel.CENTER);
lblImage.setBounds(0, 0, 800, 600);
panel.add(lblImage);
}
P.S. if the JLabel is changed to have text in it, it will display. The image is stored in a source folder called resource at the same level as the src folder.
You need to store the image in the src folder, not at the same level as it. You also need to do frame.setVisible(true) at the end of your constructor, or use revalidate(); and repaint(); to refresh your window.
I want to increase the height of a custom JPanel in a JFrame. I tried to use pa.setSize(700,200) but it does not change anything.
Here is the code:
JFrame f = new JFrame("Hello");
f.setResizable(true);
JPanel pa = new JPanel();
JButton btn = new JButton("Exit");
pa.setBackground(Color.red);
pa.setSize(700, 200);
f.setUndecorated(true);
f.getContentPane().add(pa, BorderLayout.NORTH);
f.setSize(new Dimension(700,700));
f.setLocation(500, 500);
f.setVisible(true);
its very simple solution instead of pa.setsize() change it into pa.setPreferredSize(new Dimension(width,60));
This program is supposed to open a window, add a picture, and then add the text "hello world" above the picture. The text appears when i do frame.add(label) and then try to add the picture (like the code shows), but even when I do the opposite and add the picture first I only get a gray schreen. Can anybody show me how I can get both the picture and the text?
public window(){
JFrame frame = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(600, 400);
frame.setVisible(true);
label.setAlignmentX(0);
label.setAlignmentY(0);
frame.add(label);
frame.add(new JLabel(new ImageIcon("file")));;
}
}
You should use overlay layout, but it is applicable on JPanel.
So add a JPanel to your frame then apply the layout, finally add the components.
Your code may be like that:
public window(){
JFrame frame = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
frame.setResizable(false);
frame.setSize(600, 400);
frame.setVisible(true);
label.setAlignmentX(0);
label.setAlignmentY(0);
panel.add(label);
panel.add(new JLabel(new ImageIcon("file")));
frame.add(panel, BorderLayout.CENTER);
}
}
A label can have both text and icon, and the relative position can be customized.
JLabel label = new JLabel ("hello world", new ImageIcon("file"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
//frame.add(new JLabel(new ImageIcon("file")));;
The default layout is BorderLayout, and add(label, BorderLayout.CENTER).
So I'm trying to create a gui, I've tinkered with gui's before in java but I'm still new to them. So my issued here is that my JLabels (butLabel & cbLabel) are filled with buttons and checkboxes. Sadly my JFrame will only show whichever is set to the BorderLayout.CENTER. NORTH & SOUTH don't ever show, even if I only set the butLabel to SOUTH and don't even use the cbLabel. What am I overlooking?? It's much appreciated, thanks!
public class mainWindow
{
JFrame frame = new JFrame("Main Window");
JLabel butLabel = new JLabel();
JLabel cbLabel = new JLabel();
JButton showBut = new JButton("Show");
JButton exitBut = new JButton("Exit");
JButton addBut = new JButton("Add");
JButton remBut = new JButton("Remove");
JCheckBox aCB = new JCheckBox("Airplane");
JCheckBox bCB = new JCheckBox("Boat");
JCheckBox cCB = new JCheckBox("Clock");
public mainWindow()
{
frame.setLayout(new BorderLayout()); //I know this is set by default to BorderLayout but I just did it when I was out of options to try.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(360, 480));
butLabel.setLayout(new GridLayout(1,4));
cbLabel.setLayout(new GridLayout(2, 2));
butLabel.add(showBut);
butLabel.add(exitBut);
butLabel.add(addBut);
butLabel.add(remBut);
cbLabel.add(aCB);
cbLabel.add(bCB);
cbLabel.add(cCB);
frame.add(butLabel, BorderLayout.CENTER);
frame.add(cbLabel, BorderLayout.NORTH);
}
public void setVisible()
{
butLabel.setVisible(true);//Didn't think I needed butLabel.setVisible or the cbLabel.setVisible but
cbLabel.setVisible(true);//again I was trying things that I thought might make sense.
frame.setVisible(true);
}
}
do not use Label for grouping elements, use JPanel instead
I have tried replace all
Label
with
Panel
it works