how to move JTabbedpane down - java

I want my JTabbedpane on top of panel3 and panel4. Rightnow, JTabbedpane is on top of panel1 and panel2 but I want it on top of panel3 and panel4. I try setBounds to bring JTabbedpane down but it doesn't work. Thanks for everyone help.
import java.awt.*;
import static java.awt.Font.BOLD;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import static javax.swing.SwingConstants.CENTER;
import javax.swing.event.*;
public class hotels extends JFrame{
JButton hotel;
JLabel image;
JTabbedPane tabbed,tabbed1;
JPanel panel;
JPanel panel1;
Container pane;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JLabel departure;
JLabel from;
JLabel to;
JLabel searchflight,searchflight1;
JButton back, back1;
public hotels(){
panel=new JPanel();
panel.setBackground(Color.cyan);
hotel=new JButton();
hotel.setText("Hotels");
Font myFont = new Font("Serif", Font.BOLD, 18);
hotel.setFont(myFont);
panel.setLayout(null);
panel.add(hotel);
hotel.setBounds(50, 80, 100, 40);
image=new JLabel();
image.setBounds(50,1,80,80);
image.setBorder(BorderFactory.createLineBorder(Color.yellow));
image.setBackground(Color.white);
image.setIcon(new ImageIcon("2.gif"));
panel.add(image);
panel1=new JPanel();
panel1.setLayout(null);
panel1.setBounds(0, 30, 300, 400);
back=new JButton();
back.setText("<");
back.setBounds(0, 5, 60, 30);
panel1.add(back);
searchflight=new JLabel();
searchflight.setText("Search Flights");
searchflight.setBounds(130,5,100,30);
panel1.add(searchflight);
tabbed=new JTabbedPane();
tabbed.add( "Round Trip",panel1);
panel2=new JPanel();
panel2.setLayout(null);
panel2.setBounds(0, 30, 300, 400);
panel2.setBackground(Color.blue);
searchflight1=new JLabel();
searchflight1.setText("Search Flights");
searchflight1.setBounds(130,5,100,30);
panel2.add(searchflight1);
back1=new JButton();
back1.setText("<");
back1.setBounds(0, 5, 60, 30);
panel2.add(back1);
panel4=new JPanel();
panel4.setLayout(null);
panel4.setBackground(Color.white);
panel4.setBounds(0, 40, 290, 300);
panel4.setBorder(BorderFactory.createMatteBorder(0, 10, 10, 10, Color.blue));
Font f=new Font("Serif", Font.PLAIN,12);
tabbed.add("One Way",panel2);
panel3=new JPanel();
panel3.setBounds(0, 40, 290, 300);
panel3.setLayout(null);
panel3.setBorder(BorderFactory.createMatteBorder(0, 10, 10, 10, Color.blue));
panel3.setBackground(Color.white);
panel1.add(panel3);
panel2.add(panel4);
from=new JLabel();
from.setBackground(Color.blue);
panel1.setBackground(Color.blue);
pane=getContentPane();
pane.add(panel);
image.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e){
if (e.getSource()==image){
pane.removeAll();
pane.add(tabbed);
pane.revalidate();
pane.repaint();
}
}
}
);
}
public static void main(String[] args) {
hotels mw=new hotels();
mw.setVisible(true);
mw.setSize(300, 400);
mw.setResizable(false);
}
}

Related

How to add spacing between JPanel and JFrame's contentPane?

This is the picture I am trying to replicate
This is what I have (didn't add icon images yet)
I can't seem to find a solution, been staring at it for quite some time.
I am trying to replicate the following picture, using GridLayout for the buttons and the figure out the rest on my own using Java Swing. Furthermore, I've added my buttons into a JPanel and now I'm trying to add spacing between the panel and the pane.
This is what I have, how can I go about it?
super(title);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
Container pane = this.getContentPane();
JButton b1 = new JButton();
b1.setBackground(Color.white);
JButton b2 = new JButton();
b2.setBackground(Color.white);
JButton b3 = new JButton();
b3.setBackground(Color.white);
JButton b4 = new JButton();
b4.setBackground(Color.white);
JButton b5 = new JButton();
b5.setBackground(Color.white);
JButton b6 = new JButton();
b6.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,3,10,10));
panel.setBackground(Color.black);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
pane.add(panel);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
The easiest way to do it would be to add an empty border to your JPanel (see this post on empty borders):
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 10, 10));
// ...
panel.setBorder(new EmptyBorder(50, 50, 50, 50));
Another good approach (depending always on your application needs), if you have the JButton preferred size set, would be to have the main JPanel's grid layout set to have two columns and one row, with another JPanel inside each column. Adding to the interior JPanels a BoxLayout in Y_AXIS mode and aligning the buttons with setAlignmentX() would work great too (note this approach wouldn't center the JButtons vertically) (see How to use BoxLayout):
public class MyFrame extends JFrame {
private String title = "Title";
public MyFrame(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,2,10,10));
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
JButton b1 = new JButton();
b1.setBackground(Color.white);
//b1.setIcon(new ImageIcon(img1));
b1.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b1);
JButton b2 = new JButton();
b2.setBackground(Color.white);
//b2.setIcon(new ImageIcon(img2));
b2.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b2);
JButton b3 = new JButton();
b3.setBackground(Color.white);
//b3.setIcon(new ImageIcon(img3));
b3.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(b3);
JButton b4 = new JButton();
b4.setBackground(Color.white);
//b4.setIcon(new ImageIcon(img4));
b4.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b4);
JButton b5 = new JButton();
b5.setBackground(Color.white);
//b5.setIcon(new ImageIcon(img5));
b5.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b5);
JButton b6 = new JButton();
b6.setBackground(Color.white);
//b6.setIcon(new ImageIcon(img6));
b6.setAlignmentX(Component.LEFT_ALIGNMENT);
rightPanel.add(b6);
add(mainPanel); //Adding our mainPanel to the contentPane of the JFrame
this.setSize(500,500); //or pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle(title);
this.setVisible(true);
}
}
Here's a little demonstration I whipped up.
All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
You don't set the size of the JFrame and try and make the Swing components fit. You let the JFrame pack with all the Swing components.
You create a GridLayout JPanel inside of a FlowLayout JPanel. The FlowLayout JPanel uses an empty border of the appropriate size.
I used the image the OP provided to get the icons.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class EmptySpaceDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new EmptySpaceDemo());
}
private Image[] images;
public EmptySpaceDemo() {
this.images = createImages();
}
private Image[] createImages() {
BufferedImage image = readImage();
Image[] images = new Image[6];
images[0] = image.getSubimage(155, 113, 110, 90);
images[1] = image.getSubimage(276, 113, 110, 90);
images[2] = image.getSubimage(155, 217, 110, 90);
images[3] = image.getSubimage(276, 217, 110, 90);
images[4] = image.getSubimage(155, 321, 110, 90);
images[5] = image.getSubimage(276, 321, 110, 90);
return images;
}
#Override
public void run() {
JFrame frame = new JFrame("Empty Space Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.BLACK);
panel.setBorder(BorderFactory.createEmptyBorder(40, 100, 40, 100));
JPanel innerPanel = new JPanel(new GridLayout(0, 2, 10, 10));
innerPanel.setBackground(Color.BLACK);
for (int i = 0; i < images.length; i++) {
JButton button = new JButton(new ImageIcon(images[i]));
innerPanel.add(button);
}
panel.add(innerPanel);
return panel;
}
private BufferedImage readImage() {
try {
return ImageIO.read(getClass().getResourceAsStream("/icons.png"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

How do I position my title on top of the GUI window?

I am new to programming and still learning. I need help in positioning a JLabel "Welcome back" on top of the GUI interface. The label keeps getting stuck in the middle despite my efforts. Feel free to correct any other mistakes shown in the code that I can do better.
public class MainMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
JPanel panel = new JPanel();
panel.setBounds(30, 80, 400, 570);
panel.setLayout(new GridLayout(3, 2, 15, 15));
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JButton meals = new JButton("Meals");
meals.setFont(new Font("Helvetica", Font.BOLD, 24));
meals.setOpaque(true);
JButton reminder = new JButton ("Reminders");
reminder.setFont(new Font("Helvetica", Font.BOLD, 24));
reminder.setOpaque(true);
JButton shop = new JButton ("Shop");
shop.setFont(new Font("Helvetica", Font.BOLD, 24));
shop.setOpaque(true);
JButton sleep = new JButton ("Sleep Timer");
sleep.setFont(new Font("Helvetica", Font.BOLD, 24));
sleep.setOpaque(true);
JButton account = new JButton ("My Account");
account.setFont(new Font("Helvetica", Font.BOLD, 24));
account.setOpaque(true);
JButton aboutus = new JButton ("About Us");
aboutus.setFont(new Font("Helvetica", Font.BOLD, 24));
aboutus.setOpaque(true);
JLabel label = new JLabel("Welcome back!");
label.setLocation(240, 20);
panel.add(meals);
panel.add(reminder);
panel.add(shop);
panel.add(sleep);
panel.add(account);
panel.add(aboutus);
frame.add(panel);
frame.add(label);
frame.setSize(480, 720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Panels are rearranged on an intermediary Layout-manager(Border)
There are also various solutions based on Layout-used, but the main idea is the same
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
//define a new layout
frame.setLayout(new BorderLayout());
//panel for label
JPanel topPanel = new JPanel();
JPanel panel = new JPanel();
panel.setBounds(30, 80, 400, 570);
panel.setLayout(new GridLayout(3, 2, 15, 15));
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JButton meals = new JButton("Meals");
meals.setFont(new Font("Helvetica", Font.BOLD, 24));
meals.setOpaque(true);
JButton reminder = new JButton ("Reminders");
reminder.setFont(new Font("Helvetica", Font.BOLD, 24));
reminder.setOpaque(true);
JButton shop = new JButton ("Shop");
shop.setFont(new Font("Helvetica", Font.BOLD, 24));
shop.setOpaque(true);
JButton sleep = new JButton ("Sleep Timer");
sleep.setFont(new Font("Helvetica", Font.BOLD, 24));
sleep.setOpaque(true);
JButton account = new JButton ("My Account");
account.setFont(new Font("Helvetica", Font.BOLD, 24));
account.setOpaque(true);
JButton aboutus = new JButton ("About Us");
aboutus.setFont(new Font("Helvetica", Font.BOLD, 24));
aboutus.setOpaque(true);
JLabel label = new JLabel("Welcome back!");
panel.add(meals);
panel.add(reminder);
panel.add(shop);
panel.add(sleep);
panel.add(account);
panel.add(aboutus);
topPanel.add(label);
topPanel.setSize(200, 30);
//rearrange panels on frame
frame.add(topPanel, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(480, 720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output:

Exit a JPanel or a GlassPane when we click outside it

Days ago i asked for help to put a JPanel on top of the other CardLayout panels, with the help of one of the users i achieved it using GlassPane, so thanks to him, but now i wanna close it whenever i click outside it(in other windows of the applications or components) because its stuck there until i click settings button, how can i achieve that? I have tried with focus lost and gained but that doesn't work with the panel so what am i supposed to do, here is my piece of code..
JPanel settingsPanel = new JPanel();
settingsPanel.setLayout(null);
settingsPanel.setPreferredSize(
new Dimension(180, 260));
JLabel lblSettingsTitle = new JLabel("Settings");
lblSettingsTitle.setFont(new Font("SansSerif", Font.BOLD |
Font.ITALIC, 18));
lblSettingsTitle.setBounds(5, 8, 200, 35);
settingsPanel.add(lblSettingsTitle);
JSeparator settingsSep = new JSeparator();
settingsSep.setForeground(Color.DARK_GRAY);
settingsSep.setBounds(0, 48, 160, 2);
settingsPanel.add(settingsSep);
JPanel panelLanguage = new JPanel();
panelLanguage.setBounds(0, 61, 200, 30);
settingsPanel.add(panelLanguage);
panelLanguage.setLayout(null);
JPanel pnlLanguage = new JPanel();
pnlLanguage.setBounds(0, 0, 200, 30);
panelLanguage.add(pnlLanguage);
pnlLanguage.setLayout(null);
JLabel lblLanguageIcon = new JLabel("");
lblLanguageIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/Language_20px.png")));
lblLanguageIcon.setBounds(5, 5, 20, 20);
pnlLanguage.add(lblLanguageIcon);
JLabel lblLanguage = new JLabel("Choose Language");
lblLanguage.setFont(new Font("SansSerif", Font.ITALIC, 15));
lblLanguage.setBounds(30, 0, 170, 30);
pnlLanguage.add(lblLanguage);
JPanel pnlAlbanian = new JPanel();
pnlAlbanian.setBounds(0, 30, 200, 30);
panelLanguage.add(pnlAlbanian);
pnlAlbanian.setLayout(null);
JLabel lblAlbIcon = new JLabel("");
lblAlbIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/albanian.png")));
lblAlbIcon.setBounds(20, 0, 30, 30);
pnlAlbanian.add(lblAlbIcon);
JLabel lblAlbanian = new JLabel("Albanian");
lblAlbanian.setBounds(50, 0, 150, 30);
pnlAlbanian.add(lblAlbanian);
JPanel pnlEnglish = new JPanel();
pnlEnglish.setBounds(0, 60, 200, 30);
panelLanguage.add(pnlEnglish);
pnlEnglish.setLayout(null);
JLabel lblEngIcon = new JLabel("");
lblEngIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/britain.png")));
lblEngIcon.setBounds(20, 0, 30, 30);
pnlEnglish.add(lblEngIcon);
JLabel lblEnglish = new JLabel("English");
lblEnglish.setBounds(50, 0, 150, 30);
pnlEnglish.add(lblEnglish);
JPanel pnlAboutUs = new JPanel();
pnlAboutUs.setBounds(0, 120, 200, 30);
settingsPanel.add(pnlAboutUs);
pnlAboutUs.setLayout(null);
JLabel lblAboutIcon = new JLabel("");
lblAboutIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/About_20px.png")));
lblAboutIcon.setBounds(5, 5, 20, 20);
pnlAboutUs.add(lblAboutIcon);
JLabel lblAboutUs = new JLabel("About Us");
lblAboutUs.setFont(new Font("SansSerif", Font.ITALIC, 15));
lblAboutUs.setBounds(35, 0, 165, 30);
pnlAboutUs.add(lblAboutUs);
JPanel pnlHelp = new JPanel();
pnlHelp.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
Help obj=new Help();
getGlassPane().setVisible(false);
obj.setVisible(true);
obj.setLocationRelativeTo(null);
}
});
pnlHelp.setLayout(null);
pnlHelp.setBounds(0, 91, 200, 30);
settingsPanel.add(pnlHelp);
JLabel lblHelpIcon = new JLabel("");
lblHelpIcon.setIcon(new
ImageIcon(frmMain.class.getResource("/image/Help_20px.png")));
lblHelpIcon.setBounds(5, 5, 20, 20);
pnlHelp.add(lblHelpIcon);
JLabel lblHelp = new JLabel("Help");
lblHelp.setFont(new Font("SansSerif", Font.ITALIC, 15));
lblHelp.setBounds(35, 0, 165, 30);
pnlHelp.add(lblHelp);
((JComponent) getGlassPane()).setLayout(new
FlowLayout(FlowLayout.LEFT, 260, 390));
((JComponent) getGlassPane()).add(settingsPanel, BorderLayout.EAST);
JLabel lblSettings = new JLabel("");
lblSettings.setHorizontalAlignment(SwingConstants.CENTER);
lblSettings.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
getGlassPane().setVisible(!getGlassPane().isVisible());
}
});
lblSettings.setToolTipText("Settings");
lblSettings.setIcon(new
ImageIcon(frmMain.class.getResource("/image/Settings_24px.png")));
lblSettings.setBounds(210, 600, 50, 50);
menuPanel.add(lblSettings);
I've slightly modified my previous example, so the settings panel goes closed on the click outside it.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class RightSidePanel implements Runnable {
#Override
public void run() {
JFrame frm = new JFrame("Right side panel");
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// next two lines are not required
JPanel contentPanel = new JPanel(new BorderLayout());
frm.setContentPane(contentPanel);
JPanel mainPanel = new JPanel(new CardLayout());
mainPanel.add(new JLabel("It's the first card panel"), "first");
mainPanel.add(new JLabel("It's the second card panel"), "second");
// add some components to provide some width and height for the panel.
mainPanel.add(Box.createHorizontalStrut(600));
mainPanel.add(Box.createVerticalStrut(300));
mainPanel.setBackground(Color.CYAN);
JPanel settingsPanel = new JPanel(new GridLayout(1, 1));
settingsPanel.add(new JLabel("Here is the settings panel!"));
settingsPanel.setPreferredSize(new Dimension(settingsPanel.getPreferredSize().width, 300));
JButton settingsButton = new JButton("Show settings"); // move this line up
((JComponent) frm.getGlassPane()).setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
((JComponent) frm.getGlassPane()).add(settingsPanel);
// added code here
frm.getGlassPane().addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
// check whether the click on the glass pane.
Component c = SwingUtilities.getDeepestComponentAt(frm.getGlassPane(), e.getX(), e.getY());
if (e.getComponent().equals(c)) {
updateButton(frm, settingsButton);
}
}
});
// end of the added code
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
settingsButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// move the method content to a separate method
updateButton(frm, settingsButton);
}
});
JButton switchButton = new JButton("Show second");
switchButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) mainPanel.getLayout();
if (mainPanel.getComponent(0).isVisible()) {
cl.show(mainPanel, "second");
switchButton.setText("Show first");
} else {
cl.show(mainPanel, "first");
switchButton.setText("Show second");
}
}
});
buttonPanel.add(switchButton);
buttonPanel.add(settingsButton);
frm.add(mainPanel, BorderLayout.CENTER);
frm.add(buttonPanel, BorderLayout.SOUTH);
frm.pack();
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
private void updateButton(JFrame frm, JButton settingsButton) {
frm.getGlassPane().setVisible(!frm.getGlassPane().isVisible());
if (frm.getGlassPane().isVisible()) {
settingsButton.setText("Hide settings");
} else {
settingsButton.setText("Show settings");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new RightSidePanel());
}
}

Position glCanvas alongside a JPanel containing buttons

I'd like to have the JButton panel consume ~30% of the frames horizontal space.
And glCanvas on the right side taking up the rest of the frames space.
How can I achieve this layout?
Currently:
Main.java
GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);
GLCanvas glcanvas = new GLCanvas(capabilities);
glcanvas.addGLEventListener(new GameRenderer());
glcanvas.setSize(100, 100);
JFrame frame = new JFrame("Tool");
JPanel panel = new JPanel();
JPanel cpanel=new JPanel();
panel.setLayout(null);
cpanel.setLayout(null);
JButton ButtonBR = new JButton("1");
JButton ButtonE = new JButton("2");
JButton ButtonR = new JButton("3");
JButton ButtonC = new JButton("4");
JButton ButtonT = new JButton("5");
JButton ButtonCR = new JButton("6");
ButtonBR.setBounds(10, 30, 150, 40);
ButtonE.setBounds(10, 80, 150, 40);
ButtonR.setBounds(10, 130, 150, 40);
ButtonC.setBounds(10, 180, 150, 40);
ButtonT.setBounds(10, 230, 150, 40);
ButtonCR.setBounds(10, 450, 150, 40);
cpanel.add(glcanvas);
panel.add(ButtonBR);
panel.add(ButtonE);
panel.add(ButtonR);
panel.add(ButtonC);
panel.add(ButtonT);
panel.add(ButtonCR);
frame.add(cpanel);
frame.add(panel);
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
frame.setVisible(true);
Animator animator = new Animator(glcanvas));
animator.start();
Consider using a MigLayout as such:
import javax.swing.JFrame;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public Main() {
getContentPane().setLayout(new MigLayout("", "[grow 30][grow 70]", "[]"));
JLabel label = new JLabel("30%");
getContentPane().add(label, "cell 0 0");
JTextField textField = new JTextField("70%");
getContentPane().add(textField, "cell 1 0,growx");
pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}

Put a JTextfield on a JPanel?

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();
}
}

Categories

Resources