The radio buttons don't seem to be grouped together properly as one of them is slightly slanting towards the left. I am not sure what the error is. Everything in the code seems fine to me...I am not sure what is missing.
I have attached an image below showing the problem. The Ide I am using is NetBeans.
Thank you in advanced! :)
package pizzaorder2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.FlowLayout;
import javax.swing.SwingUtilities;
import javax.swing.JRadioButton;
public class PizzaOrder2 extends JFrame {
public static void main(String[] args) {
JFrame frame = new PizzaOrder2();
JRadioButton tomato = new JRadioButton("Tomato");
JRadioButton barbeque = new JRadioButton("Barbeque");
ButtonGroup group = new ButtonGroup();
group.add(tomato);
group.add(barbeque);
JPanel radiopanel = new JPanel();
radiopanel.add(tomato);
radiopanel.add(barbeque);
frame.getContentPane().add(radiopanel);
radiopanel.setBounds(240,330,110,70);
radiopanel.setOpaque(false);
tomato.setForeground(Color.white);
barbeque.setForeground(Color.white);
frame.setLayout(null);
frame.setSize(600, 700);
frame.getContentPane().setBackground(new Color(40, 80, 120));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Try adjusting radiopanel to this:
JPanel radiopanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
to explain a little better, you're just setting it up so items align on the left, rather than the center (which I believe is default). You will need to import FlowLayout for this.
Related
package Jframe;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JavaWindows {
public static void main(String args[]) {
JFrame Window = new JFrame();
JLabel Label = new JLabel("test");
JPanel Panel = new JPanel();
ImageIcon icon = new ImageIcon("Heart.png");
Window.setIconImage(icon.getImage());
Window.add(Panel);
Window.add(Label);
Window.setSize(500,750);
Panel.setSize(500, 900);
Window.getContentPane().setBackground(Color.PINK);
Window.pack();
}
}
About 10 min ago this code worked. It made a window that was about 100,100 with a 500,900 panel that was sized correctly. The window was not 500,750 like i specified I don't know what went wrong there but all sudden the window wont run in Eclipse. I deleted window.pack(); and retyped it now its broke. Any ideas?
you have to set visibility of the frame
window.setVisible(true);
Accordion can be downloaded here - http://www.javaswingcomponents.com/product/accordion
Here is a sample output of an accordion. I want to remove the numbers on the right side of the tab. How can I do it? Thanks!
Here is the code of the sample:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import com.javaswingcomponents.accordion.JSCAccordion;
import com.javaswingcomponents.accordion.TabOrientation;
public class SampleAccordion extends JPanel {
static JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SampleAccordion codeExample = new SampleAccordion();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container panel = frame.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(codeExample, BorderLayout.CENTER);
frame.pack();
frame.setSize(500, 300);
frame.setVisible(true);
}
});
}
public SampleAccordion() {
JSCAccordion accordion = new JSCAccordion();
JPanel transparentPanel = new JPanel();
transparentPanel.setOpaque(false);
transparentPanel.setBackground(Color.GRAY);
JPanel opaquePanel = new JPanel();
opaquePanel.setOpaque(true);
opaquePanel.setBackground(Color.GRAY);
accordion.addTab("Tab 1", new JLabel("help me remove 1"));
accordion.addTab("Tab 2", new JLabel("help me remove 2"));
accordion.setTabOrientation(TabOrientation.VERTICAL);
setLayout(new GridLayout(1, 1, 30, 30));
add(accordion);
}
}
You can specify whether you want to see the tab index:
accordion.setTabOrientation(TabOrientation.VERTICAL);
((FormattedTabRenderer) accordion.getTabRenderer()).setShowIndex(false);
(The first line is already in the sample code and is only included as a reference.)
It looks like the accordion supports three pluggable look & feels: basic, steel, and dark steel. I'm not sure whether the tab renderer can be cast to the FormattedTabRenderer abstract class for all PLAFs, but it seems to work fine for steel.
My overall goal is to create a jigsaw puzzle game that moves a set of images around the GUI screen with mousedrag. My current issue is to make this snapping function that when the mouse dragging the image releases, the image will snap into a predetermined place. This is where I want to create "boxes". I want to create 16 boxes for the 16 puzzle pieces and use their coordinates as the snapping location for the images when I release the mouse. (Just how in puzzle games the pieces snap to the correct place when you hover a general area). I am attempting this by making 16 JPanels, but I don't know if this is the smart way. Also, it seems I can't even add the JPanels to my JLayeredPane. I tried to set a border for one of the JPanels to help me see if it was added, and it was not.
Mouse drag works fine, everything works fine. I just want to be able to add some sort of representation of a box that the images when released by the mouse will snap into the box's location. Please let me know if you have any insights to this problem!
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Gui1 {
public static void main(String[] args) {
new Gui1();
}
public Gui1() { //Constructor for setting up the GUI
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JLayeredPane pane = new JLayeredPane();
JFrame frame = new JFrame("Testing");
JPanel canvas1 = new JPanel();
JPanel canvas2 = new JPanel();
JPanel canvas3 = new JPanel();
JPanel canvas4 = new JPanel();
JPanel canvas5 = new JPanel();
JPanel canvas6 = new JPanel();
JPanel canvas7 = new JPanel();
JPanel canvas8 = new JPanel();
JPanel canvas9 = new JPanel();
JPanel canvas10 = new JPanel();
JPanel canvas11 = new JPanel();
JPanel canvas12 = new JPanel();
JPanel canvas13 = new JPanel();
JPanel canvas14 = new JPanel();
JPanel canvas15 = new JPanel();
JPanel canvas16 = new JPanel();
canvas1.setPreferredSize(new Dimension(300,300));
canvas1.setBorder(BorderFactory.createLineBorder(Color.blue,10));
pane.add(canvas1, JLayeredPane.DEFAULT_LAYER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ImageGrab());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
I want to change the colour of the jTabbed Pane header(tab headers) and i want to get rid of the line which is separating the tab header and its body. I also like to have round edges for Tabbed Pane. How to do all these customization in swing? Is there any external jars available with this kind of inbuilt gui features. I don't want to use any look and feel to do this because many of them are also not customizable according to my need. As i am quite new to java please give me a detailed answer. Thanks in advance.
For more complex look-and-feel changes you should take a look into writing your own look-and-feel like glad3r already mentioned.
This post also is a great example.
For some basic things you might simply want to change some UIManager values, see example below and play a bit around with those values.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.plaf.BorderUIResource;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.InsetsUIResource;
#SuppressWarnings("serial")
public class TabbedPaneTest extends JTabbedPane {
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPane");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.RED);
frame.getContentPane().add(new TabbedPaneTest());
frame.setVisible(true);
}
public TabbedPaneTest() {
UIManager.put("TabbedPane.contentBorderInsets", new InsetsUIResource(1, 0, 0, 0));
UIManager.put("TabbedPane.contentAreaColor", new ColorUIResource(Color.GREEN));
UIManager.put("TabbedPane.focus", new ColorUIResource(Color.ORANGE));
UIManager.put("TabbedPane.selected", new ColorUIResource(Color.YELLOW));
UIManager.put("TabbedPane.darkShadow", new ColorUIResource(Color.DARK_GRAY));
UIManager.put("TabbedPane.borderHightlightColor", new ColorUIResource(Color.LIGHT_GRAY));
UIManager.put("TabbedPane.light", new ColorUIResource(Color.WHITE));
UIManager.put("TabbedPane.tabAreaBackground", new ColorUIResource(Color.CYAN));
UIManager.put("ToolTip.background", Color.WHITE);
UIManager.put("ToolTip.border", new BorderUIResource(new LineBorder(Color.BLACK)));
this.updateUI();
this.setBackground(Color.BLUE);
JPanel testPanel = new JPanel();
testPanel.setLayout(new BorderLayout());
testPanel.add(new JLabel("Hello World"), BorderLayout.NORTH);
testPanel.add(new JTextArea("Looks nice out there :)"), BorderLayout.CENTER);
JPanel testPanel2 = new JPanel();
testPanel2.setLayout(new BorderLayout());
testPanel2.add(new JLabel("Good Bye World"), BorderLayout.NORTH);
testPanel2.add(new JTextArea("AAAAAnnnnnd... I'm gone"), BorderLayout.CENTER);
this.addTab("Hello World", testPanel);
this.addTab("BB World", testPanel2);
}
}
If I have code like so:
class X extends JFrame
{
X()
{
setLayout(new GridLayout(3,3));
JButton b = new JButton("A-ha");
/*I would like to add this button in the center of this grid (2,2)*/
//How can I do it?
}
};
As what I know, you have to fill all the previous cells before. So you need to add 4 components before you can add the center component. Hmm, it could be a better layoutmanager.
What are you trying to do? Maybe BorderLayout is what you are looking for?
You might want to take a look at the GridBag Layout
This centres vertically and horizontally
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class centerbutton extends JFrame{
public centerbutton(){
setLayout(new BorderLayout());
JPanel panel = new JPanel();
JButton button = new JButton("A-ha!");
button.setAlignmentX(
Component.CENTER_ALIGNMENT);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(Box.createVerticalGlue());
panel.add(button);
panel.add(Box.createVerticalGlue());
getContentPane().add(panel);
setVisible(true);
}
public static void main(String[] args) {
new centerbutton();
}
}