Why the textfield is not appearing on my panel which is inside my frame?
I mean is there some additional action necessary to make the components of the panel
visible?
I hope somebody can help me....
public class example1 {
public static void main(String[] args) {
JFrame tt=new TT();
}
}
class TT extends JFrame {
JTextField textField;
JPanel panel;
JButton button1;
JButton button2;
public TT() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Bla Blubb");
setResizable(false);
setLayout(null);
panel=new JPanel();
panel.setBounds(5, 5, 290, 290);
add(panel);
textField=new JTextField();
textField.setBounds(5, 5, 280, 50);
panel.add(textField);
setVisible(true);
}
}
import java.awt.FlowLayout;
import javax.swing.*;
class TT extends JFrame {
JTextField textField;
JPanel panel;
JButton button1;
JButton button2;
public TT() {
//setSize(300, 300); // better to use pack() (after components added)
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // better to use
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//setLocationRelativeTo(null); // better to use..
setLocationByPlatform(true);
setTitle("Bla Blubb");
setResizable(false);
//setLayout(null); // better to use layouts with padding & borders
// set a flow layout with large hgap and vgap.
panel = new JPanel(new FlowLayout(SwingConstants.LEADING, 10, 10));
// panel.setBounds(5, 5, 290, 290); // better to pack()
add(panel);
//textField = new JTextField(); // suggest a size in columns
textField = new JTextField(8);
//textField.setBounds(5, 5, 280, 50); // to get height, set large font
textField.setFont(textField.getFont().deriveFont(50f));
panel.add(textField);
pack(); // make the GUI the minimum size needed to display the content
setVisible(true);
}
public static void main(String[] args) {
// GUIS should be constructed on the EDT.
JFrame tt = new TT();
}
}
Related
So Im trying to make a little program to calculate the area of a specific shape.
The user should be able to make a input via a textfield (Like the height and stuff of the shapes). The he should press a button and the price should get printed.
But it doesnt show up.
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Rechner extends JFrame implements ActionListener{
private static JButton button1;
private static JButton button2;
private static JButton button3;
private static JButton button4;
private static JTextField numberField;
private JPanel jpanel;
public Rechner(String titel){
super(titel);
jpanel = new JPanel();
numberField = new JTextField(1500);
add(numberField, BorderLayout.CENTER);
button1 = new JButton("Rechteck");
button1.setBounds(10, 10, 150, 30);
button1.addActionListener(this);
add(button1);
button2 = new JButton("Dreieck");
button2.setBounds(170, 10, 150, 30);
button2.addActionListener(this);
add(button2);
button3 = new JButton("Trapez");
button3.setBounds(330, 10, 150, 30);
button3.addActionListener(this);
add(button3);
button4 = new JButton("Parallelogramm");
button4.setBounds(490, 10, 150, 30);
button4.addActionListener(this);
add(button4);
setResizable(false);
}
public static void main(String[] args) {
Rechner frame = new Rechner("Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(660, 400);
frame.setLayout(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
System.out.println("fff");
}
String numberStr = numberField.getText();
}
}
The default layout manager of a JFrame (well, of its content pane in fact) is BorderLayout.
Without specified constraints , the component is added to BorderLayout.CENTER, so
add(component);
is the same as
add(component, BorderLayout.CENTER);
and each component added this way will replace the last component added to the center.
Also note that setBounds will have no effect if there is a layout manager, and that you create a JPanel that you never use.
Finally, you may want to have a look at this guide : A Visual Guide to Layout Managers
This line is mainly the problem:
add(numberField, BorderLayout.CENTER);
Is causing the TextField to fill the entire space. Then, the next time you add a component to the JFrame with BorderLayout.CENTER, the JTextField gets replaced. To fix this:
super(titel);
jpanel = new JPanel();
add(jpanel, BorderLayout.NORTH); //adding the jpanel
button1 = new JButton("Rechteck");
jpanel.add(button1);
button1.setBounds(10, 10, 150, 30);
//adding the other buttons to the JPanel...
//...
//...
button4.addActionListener(this);
button3.addActionListener(this);
button2.addActionListener(this);
button1.addActionListener(this);
numberField = new JTextField(1500);
add(numberField);//this will cause it to fill the remaining space
setResizable(false);
Explanation:
The buttons should go into the JPanel you created, and the JPanel should go into the JFrame's NORTH. That way they don't cover the JFrame
How do I add all of these component in a JPanel by using MigLayout and achieve the rest of the conditions as described in the above picture?
Using MigLayout you can simply add a JSeparator to the adjacent cell, giving it the growx property. For instance:
JLabel lblPersonal = new JLabel("Personal");
contentPane.add(lblPersonal, "cell 0 0");
contentPane.add(new JSeparator(), "cell 1 0,growx");
Or, perhaps a nicer way, is to use a border on a panel, while giving the panel a title as such:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import net.miginfocom.swing.MigLayout;
public class TitledPanel extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TitledPanel frame = new TitledPanel();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TitledPanel() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[grow]", "[grow]"));
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY), "Personal"));
contentPane.add(panel, "cell 0 0,grow");
JLabel lblLabel = new JLabel("Label 1");
panel.add(lblLabel);
textField = new JTextField();
panel.add(textField);
textField.setColumns(10);
pack();
}
}
Now, if you want to create a function that returns such panels when you pass a string parameter, you could extend a JPanel or create your own class that returns your custom-created JPanel with the given title label and separator.
What is wrong with my code? My buttons and the labels are not appearing.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HelloPanelLabel extends JFrame {
public static void main(String[] args) {
new HelloPanelLabel(); // creates an instance of frame class
}
public HelloPanelLabel() {
this.setSize(200, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Hello World!");
this.setVisible(true);
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d= tk.getScreenSize();
int x=(d.height/2);
int y=(d.width/2);
this.setLocation(x, y);
//JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("hello, world");
//panel1.add(label1);
JButton button1 = new JButton("Click me!");
//panel1.add(button1);
this.setVisible(true);
}
}
The reason for not showing of JButton and JLabel is that you have not added the JPanel containing these two components to the JFrame.You just need a little modification in your code. Here is that:
panel1.add(label1);
JButton button1 = new JButton("Click me!");
panel1.add(button1);
getContentPane().add(panel1);//Add to ContentPane of JFrame
this.setVisible(true);
And remove the the previous this.setVisible(true) line in your programe.
You need to set a layout and add your components to the frame.
setLayout(new FlowLayout());
//JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("hello, world");
add(label1);
//panel1.add(label1);
JButton button1 = new JButton("Click me!");
add(button1);
//panel1.add(button1);
this.setVisible(true);
Like the comments stated, you have to call pack(). However, if you want to define more complex layouts, you will have to create a more complex.
If you want to use a JPanel for your components
public class HelloPanelLabel extends JFrame {
public static void main(String[] args) {
new HelloPanelLabel().setVisible(true);
}
public HelloPanelLabel() {
//The same as setTitle.
super("Hello World!");
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("hello, world");
panel1.add(label1);
JButton button1 = new JButton("Click me!");
panel1.add(button1);
add(panel1);
//Size the frame to fit the components
pack();
//Center the frame.
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
alternatively you can add them directly to the contentPane of the JFrame.
setLayout(new FlowLayout());
JLabel label1 = new JLabel("hello, world");
add(label1);
JButton button1 = new JButton("Click me!");
add(button1);
Toolkit theKit = getToolkit();
Dimension wndSize = theKit.getScreenSize();
setSize(wndSize.width / 8, wndSize.height / 12);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I use this code to set a picture to jframe .
But , now my Jtable don't show, why?
public class samplepage extends JFrame{
private AllUser userModel;
private JTable uTable;
JButton addUser;
JButton deleteUser;
JButton mainButton;
JTextField tf1;
JButton searchButton;
JLabel resultLbl;
public samplepage(){
userModel=new AllUser();
uTable=new JTable(userModel);
add(new JScrollPane(uTable), BorderLayout.CENTER);
add(buttonPanels() , BorderLayout.PAGE_START);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
this.setLocation(300, 60);
this.setResizable(false);
}
public final JPanel buttonPanels(){
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
setPic(this);
addUser=new JButton("Add New User");
deleteUser=new JButton("delete User");
mainButton= new JButton("Main Page");
tf1 = new JTextField(" ");
searchButton=new JButton("Search");
resultLbl=new JLabel();
buttonsPanel.add(addUser);
buttonsPanel.add(deleteUser);
buttonsPanel.add(mainButton);
buttonsPanel.add(tf1);
buttonsPanel.add(searchButton);
buttonsPanel.add(resultLbl);
return buttonsPanel;
}
public void setPic(JFrame jframe){
try{
File f=new File("pics\\user_group.png");
BufferedImage img=ImageIO.read(f);
jframe.setContentPane(new SetMyImage(img));
}
catch(IOException ioe){
}
}
public static void main(String[] args){
new samplepage().setVisible(true);
}
}
second Class:
public class SetMyImage extends JPanel{
private Image img;
public SetMyImage(Image img){
this.img=img;
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, 00, 00, null);
}
}
Where is my mistake?
uTable=new JTable(userModel);
add(new JScrollPane(uTable), BorderLayout.CENTER);
add(buttonPanels() , BorderLayout.PAGE_START);
First you add the table to the content pane. Then you create your buttonPanels() which invokes the setPic(...) method which creates your image panel and makes it the content pane of your frame so you lose the table.
Remove the setPic() code from your buttonPanels code. Instead your code should be something like:
setPic(...);
uTable=new JTable(userModel);
add(new JScrollPane(uTable), BorderLayout.CENTER);
add(buttonPanels() , BorderLayout.PAGE_START);
Also, you need to set the layout of your image panel to be a BorderLayout.
Since you are painting your image at its actual size there is no need to create a custom panel to do the painting. Instead you can just use a JLabel as your content pane. For more information on this approach see Background Panel.
Been sitting here at my computer for about 13 hours and I think my eyes are bleeding.
I found a little gui editor I love called GuiGenie.
It works perfect for creating the window with the buttons and all that good stuff.
The problem is i want to click a button in my first menu and have it open my other menu i made.
I just starting programming 4 weeks ago so I'm a complete noob.
I have a feeling its messing up because of the main methods but I have no idea and 13 hours of sitting here trying millions of things is making me go crazy : )
here is what i got so far
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
public MyPanel() {
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("How long were you parked?");
jcomp3 = new JLabel ("Minutes");
jcomp4 = new JButton ("openNewWindow");
//adjust size and set layout
setPreferredSize (new Dimension (315, 85));
setLayout (null);
//add components
add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
//set component bounds (only needed by Absolute Positioning)
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setBounds (0, 0, 315, 25);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel());
frame.pack();
frame.setVisible (true);
}
}
When the button is pressed, I want it to open this new window
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel2 extends JPanel {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public MyPanel2() {
//construct components
jcomp1 = new JButton ("test1");
jcomp2 = new JButton ("test2");
jcomp3 = new JButton ("test3");
jcomp4 = new JTextField (5);
//adjust size and set layout
setPreferredSize (new Dimension (395, 156));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (20, 45, 100, 25);
jcomp2.setBounds (135, 60, 100, 25);
jcomp3.setBounds (260, 35, 100, 25);
jcomp4.setBounds (105, 115, 100, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel2());
frame.pack();
frame.setVisible (true);
}
}
If anyone could help I would appreciate it greatly!!
I have a lot of respect for you pros out there because if you are a pro at this, you are probably smarter than 99.9% of the world.
This stuff hurts my brain.
Here is something you can do, for this situation, where you have multiple Forms or Windows what you can do is to use a JPanel which can have this CardLayout set as it's LayoutManager and then you can add the two JPanels to it and access them with the methods provided by the same.
Don't use setBounds() when using Absolute Positioning this is really not the right way of putting components to the parent container. Instead use setLocation(...) and setSize(...) methods. Consider not to use Absolute Positioning as much as possible for you. Certain lines in favour of the before said line taken from Java Docs are as follows :
Although it is possible to do without a layout manager, you should use a
layout manager if at all possible. A layout manager makes it easier to
adjust to look-and-feel-dependent component appearances, to different
font sizes, to a container's changing size, and to different locales.
Layout managers also can be reused easily by other containers, as well as
other programs.
Since the output of your program is really not a soothing experience in any sense. Atleast LayoutManager, can make that work a lot more easier for you, since you need not have to specify position and size for each and every component. Try walking through the Layout Mangers Tutorials, and get accustomed to them as soon as possible. They are the real life savers :-)
Here is a modified code taken from your SOURCE CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel2 panel2;
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane);
panel2 = new MyPanel2();
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
private JPanel contentPane;
public MyPanel(JPanel panel) {
contentPane = panel;
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("How long were you parked?");
jcomp3 = new JLabel ("Minutes");
jcomp4 = new JButton ("openNewWindow");
//adjust size and set layout
setPreferredSize (new Dimension (315, 85));
setLayout (null);
//set component bounds (only needed by Absolute Positioning)
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setLocation(0, 0);
jcomp4.setSize(315, 25);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
//add components
add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
}
class MyPanel2 extends JPanel {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public MyPanel2() {
//construct components
jcomp1 = new JButton ("test1");
jcomp2 = new JButton ("test2");
jcomp3 = new JButton ("test3");
jcomp4 = new JTextField (5);
//adjust size and set layout
setPreferredSize (new Dimension (395, 156));
setLayout (null);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (20, 45, 100, 25);
jcomp2.setBounds (135, 60, 100, 25);
jcomp3.setBounds (260, 35, 100, 25);
jcomp4.setBounds (105, 115, 100, 25);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
}
Here is the code for myPanel class, use this one:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
public MyPanel() {
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("How long were you parked?");
jcomp3 = new JLabel ("Minutes");
jcomp4 = new JButton ("openNewWindow");
jcomp4.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel2());
frame.pack();
frame.setVisible (true);
}
});
//adjust size and set layout
setPreferredSize (new Dimension (315, 85));
setLayout (null);
//add components
add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
//set component bounds (only needed by Absolute Positioning)
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setBounds (0, 0, 315, 25);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel());
frame.pack();
frame.setVisible (true);
}
}