I have a JFrame with it's content pane. JMenuBar is docked on the north of the pane and JLabel (status bar of sorts) on the south.
In the middle is a JTabbedPane. Each tab is a "document". It contains a JScrollBar and a JPanel in it's viewport.
It goes on and on (JPanel of the viewport has more JPanels, that can have more of them, etc...), but for this example, lets just say that that JPanel (in the viewport) can, or cannot fit into the window space (so it cannot, or can force scrollBars to be represented on the screen).
When it fits the window, everyting is fine, but as soon as I set it's height to be too hight to fit inside a window, JMenuBar gets squished on the top.
I'd like to prevent that (without having to specify the absolute height for the JMenuBar, it'd probably work, but it's kind of cheap), since it shouldn't happen in the first place.
Here's SCCE (It's not really short, but you only need to look at the lines 37 to 117, and I have marked all the lines that have something to do with layout with //TODO). Also, to see when problem occurs or when it doesn't occur, change height value in the line 88 inbetween 2000 and 200. You also need a MiG Layout library, of course.
Here's the code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import net.miginfocom.swing.MigLayout;
class Menu extends JMenuBar
{
private static final long serialVersionUID = 1L;
public Menu()
{
JMenu fileMenu = new JMenu("file");
this.add(fileMenu);
}
}
class DisplayGUI
{
JTabbedPane documentSelector;
void addNewDocument(String name)
{
Document newDocument = new Document();
newDocument.addChapter(new Chapter(), 1);
documentSelector.add(newDocument, name);
}
public DisplayGUI()
{
JFrame masterWindow = new JFrame("name");
masterWindow.setSize(1100, 800);
masterWindow.setMinimumSize(new Dimension(400, 400));
masterWindow.setLocationRelativeTo(null);
masterWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel rootPanel = new JPanel();
rootPanel.setLayout(new MigLayout()); //TODO Here is layout set for the content pane of the main JFrame
Menu menuBar = new Menu();
rootPanel.add(menuBar, "span, north"); //TODO Here is menu bar added to the JFrame, it's docked north
JLabel statusBar = new JLabel("Welcome to PLabScript editor! Press File>New to create a new file or go to File>Open to open an existing one.");
statusBar.setOpaque(true);
statusBar.setBorder(BorderFactory.createLoweredSoftBevelBorder());
rootPanel.add(statusBar, "span, south"); //TODO Here is status bar added to the JFrame, it's docked south
documentSelector = new JTabbedPane(JTabbedPane.NORTH); //TODO JTabbedPane set so the tab chooser is on the top
rootPanel.add(documentSelector, "grow, push"); //TODO setup so it will take up all the remaining space
addNewDocument("Brand new document");
masterWindow.setContentPane(rootPanel);
masterWindow.setVisible(true);
}
}
class Document extends JScrollPane
{
private static final long serialVersionUID = 1L;
JPanel basePanel;
//methods
void addChapter(Chapter chapter, int index)
{
basePanel.add(chapter, "grow, push, h 2000", index-1); //TODO this here adds a chapter to the basePanel of the JScrollPane which is the a representative of a single document
//TODO it height is set to 2000 (and the problem occurs), but if you reduce it enough so it fits the window, problem will dissaper
}
//constructors
public Document()
{
super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getVerticalScrollBar().setUnitIncrement(20);
basePanel = new JPanel();
basePanel.setBackground(Color.RED);
basePanel.setLayout(new MigLayout("insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components
setViewportView(basePanel);
}
}
class Chapter extends JPanel
{
private static final long serialVersionUID = 1L;
//constructors
Chapter()
{
setLayout(new MigLayout("insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components
setBackground(Color.MAGENTA);
}
}
public class Main
{
public static ResourceBundle language;
static boolean setUpLAF()
{
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
try
{
UIManager.setLookAndFeel(info.getClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
{
return false;
}
break;
}
}
return true;
}
public static void main(String[] args)
{
//SetUpLookAndFeel
setUpLAF();
//Display actual GUI
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new DisplayGUI();
}
});
}
}
Line 88 should read:
basePanel.add(chapter, "grow, push", index-1); //TODO this here adds a chapter to the basePanel of the JScrollPane which is the a representative of a single document
Line 100 should read:
basePanel.setLayout(new MigLayout("fill,insets 0")); //TODO "insets 0" is so there is no border thingy around all of the child components
Try this.
Related
I try to make a JFrame with a dynamical layout which should look like this
------------------------------------------|
| | Details |
| | |
| | |
| list | |
| | |
| | |
| | save |
-------------------------------------------
Where list is some tree structure where when I chose some element, the details of the element appear in Details. Now the Details consist of multiple JPanels which I put into a JPanel with BoxLayout. Below this JPanel I have a save button.
Since there are a lot of details I put the JPanel Details into a JScrollPane however, this scrollPane ignores its purpos and the result is that the Details panel is bigger than the window size. This is it goes till the bottom hiding the save button and the rest of it is not visible. The scrollbar never appears.
I don't know if it has todo with the fact that when an element is selected I update the JPanel's inside the JScrollPane or whatever.
Here is how I instantiate everything. First inside the right panel I add all JPanel's in firstPanel and I put a button inside the JPanel bottom. Then
setLayout(new BorderLayout());
add(bottom, BorderLayout.SOUTH);
scrollPane=new JScrollPane(firstPanel);
JPanel conScroll = new JPanel(new GridLayout());
conScroll.add(scrollPane);
add(conScroll, BorderLayout.CENTER);
On the JFrame level I instantiate the left and right side however I only put the left hand side and an empty JPanel for the right side called display.
When I select an element I update all elements inside firstPanel I call revalidate() and repaint() on the right side panel. Then on the Frame level I remove all elements of displayer and then add the right side to displayer.
As suggested I made a minimal working example (updated).
So for this minimal example I removed the left side. What is left is a JFrame EmpireEditor with a display JPanel inside, inside which I then put a unitEditor. Uniteditor contains two JPanel one in the center and one south. The panel in the center is in a JScrollPane.
package main;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
#SuppressWarnings("unused")
EmpireEditor r = new EmpireEditor();
}
}
The EmpireEditor
package main;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class EmpireEditor extends JFrame {
private JPanel display;
private UnitEditor unitEditor;
public EmpireEditor() {
super("Editor");
display = new JPanel();
unitEditor = new UnitEditor();
add(display);
display.add(unitEditor);
pack();
setVisible(true);
}
}
and here the UnitEditor a center Pannel where I just fill with a PanelNumber but that's just so that there is something inside and that we can see that the scrollbars don't appear. The Idea is that the bottom is always there and the rest should be filled with the JScrollPane.
package main;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
#SuppressWarnings("serial")
public class UnitEditor extends JPanel {
private PanelNumbers numbers = new PanelNumbers();
private JScrollPane scrollPane;
public UnitEditor() {
super();
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new BoxLayout(firstPanel, BoxLayout.Y_AXIS));
firstPanel.add(numbers);
JPanel bottom = new JPanel();
JButton save = new JButton("save");
bottom.add(save);
scrollPane=new JScrollPane(firstPanel);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(bottom,BorderLayout.SOUTH);
}
}
finally the PanelNumber but one could exchange this with anything.
package main;
import java.awt.GridLayout;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class PanelNumbers extends JPanel{
private Map<Value, JFormattedTextField> nums = new HashMap<>();
public PanelNumbers(){
super();
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
JFormattedTextField tmp;
JPanel numbers;
numbers = new JPanel();
numbers.setBorder(BorderFactory.createTitledBorder("Numbers"));
numbers.setLayout(new GridLayout(0, 6));
for (Value s : Value.values()) {
numbers.add(new JLabel(s.n()));
tmp = new JFormattedTextField(NumberFormat.getInstance());
setFocus(tmp);
numbers.add(tmp);
nums.put(s, tmp);
}
add(numbers);
}
public void setUnit(String name){
for (Value key : nums.keySet())
nums.get(key).setValue(0);
}
public void save(){
}
private int toNumber(String t) {
int res = 0;
try {
res = Integer.parseInt(t);
} catch (Exception e) {
}
return res;
}
private void setFocus(final JFormattedTextField num) {
num.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
num.selectAll();
}
});
}
});
//num.setPreferredSize(new Dimension(20, 10));
num.setMaximumSize(num.getPreferredSize());
}
public enum Value {
// Kampf
GROESSE("Groesse"),
WAFFENFERTIGKEIT("Waffenfertigkeit"),
FERNKAMPFFERTIGKEIT("Fernkampffertigkeit"),
ANGRIFFSBONUS("Angriffs Bonus"),
NAHKAMPFPANZERUNG("Panzerung nah"),
FERNKAMPFPANZERUNG("Panzerung fern"),
INITIATIVE("Initiative"),
TP("Treffer"),
// Mouvement
FELDER("Felder"),
BWS("Bewegungspunkte"),
B("B Befehl"),
A("A Befehl"),
P("P Befehl"),
H("H Befehl"),
M("Manoever"),
UEBERR("Ueberrenen"),
//Moral
FURCHTFAKTOR("Furchtfaktor"),
MORALERST("Erster Moralwert"),
MORALZWEIT("Zweiter Moralwert"),
//Rest
MAGIE("Magie"),
KONTROL("Kontrollbereich"),
STERNE("Sterne"),
ELEMENTE("Elemente"),
ELEMENTEPROFELD("Elemente pro Feld"),
KOSTEN("Kosten");
private String name;
Value(String name){
this.name = name;
}
public String n(){
return name;
}
}
}
setting the layout of display to GridLayout(1,1) solved the problem, somehow the original layout just let the inner panel become as big as it wants and hence the JScrollPane didn't feel the urge to make it self smaller and have scrollbars.
I have a JTabbed pane, which has a varying number of tabs. When the number of tabs is greater than 4, I get extra spacing/padding at the bottom of each tab panel. The picture below shows this (on the left you see the extra spacing, on the right you see no extra spacing).
Here is the exact code I used to get those pictures:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class DialogTest {
public static void main(String[] args) {
new DialogTest();
}
public DialogTest() {
JDialog dialog = new MyDialog();
dialog.pack();
dialog.setVisible(true);
}
class MyDialog extends JDialog {
public MyDialog() {
super(null, ModalityType.APPLICATION_MODAL);
final JTabbedPane tabs = new JTabbedPane();
final int numTabs = Integer.parseInt(JOptionPane.showInputDialog("Number of tabs:"));
setPreferredSize(new Dimension(400, 200));
for (int i = 1; i <= numTabs; i++) {
tabs.addTab("Tab"+i, new MyPanel(i));
}
setLayout(new BorderLayout());
add(tabs, BorderLayout.NORTH);
}
}
class MyPanel extends JPanel {
public MyPanel(int text) {
final JLabel label = new JLabel("THIS IS A PANEL" + text);
label.setFont(label.getFont().deriveFont(18f));
label.setBackground(Color.cyan);
label.setOpaque(true);
add(label);
setBackground(Color.red);
}
}
}
I've tried numerous things including many different layout managers. I can't for the life of me get rid of that extra spacing. Any help would be great.
final JTabbedPane tabs = new JTabbedPane();
tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); // ADD THIS!
The reason the other example behaves as it does is that the pane wraps the tabs to the next line & presumes that once we have gone beyond as many tabs as it might naturally display in a single line, it must increase the preferred size to include that extra line of tabs.
I wanted to add a scrollbar to my photo viewer but the it gives me the error that a non static variable cannot be referenced from a static context.
To be exact, I'm trying to add a scrollbar to a JPanel. Also, if I make JScrollPane scrollBar a static variable, then the photo wont appear. TIA
import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
public class PhotoViewer
{
// Instance fields.
private FilenameFilter fileNameFilter;
private JFileChooser fileChooser;
private JMenuBar menuBar;
private JPanel mainPanel;
private static JScrollPane scrollBar;
public PhotoViewer() // Constructor.
{
// Main JPanel with a grid style layout.
mainPanel = new JPanel(new GridLayout());
// Jlabel to display photo on.
final JLabel imageView = new JLabel();
// Adds the JLabel ontop of the JPanel.
mainPanel.add(imageView);
// Adds a scroll bar.
scrollBar = new JScrollPane(mainPanel);
scrollBar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// Creates a file chooser to find a photo.
fileChooser = new JFileChooser();
// Creates a new menubar at the top of the JPanel.
menuBar = new JMenuBar();
// Adds a menu within the JMenuBar.
JMenu menu = new JMenu("View new photo");
// Adds the additional menu ontop of the original JMenuBar.
menuBar.add(menu);
// Option to browse for a new photo.
JMenuItem browse = new JMenuItem("Browse");
// Adds the browse option ontop of the 'View new photo' button.
menu.add(browse);
// Creates an actionlistener to follow what the user is doing.
browse.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int result = fileChooser.showOpenDialog(mainPanel);
// Displays the image if approved by JFileChooser.
if (result==JFileChooser.APPROVE_OPTION)
{
// Obtains the selected file by the user.
File singleImage = fileChooser.getSelectedFile();
try
{
// Displays the image if no exception.
Image displayImage = ImageIO.read(singleImage);
imageView.setIcon(new ImageIcon(displayImage));
} catch(Exception e)
{
// Displays the exception caught by the program in a JOptionPane window.
e.printStackTrace();
JOptionPane.showMessageDialog(mainPanel, e, "Load failure!", JOptionPane.ERROR_MESSAGE);
}
}
}
});
} // end of constructor PhotoViewer
public void loadImages(File directory) throws IOException
{
// Throws an exception to be caught.
File[] imageFiles = directory.listFiles(fileNameFilter);
BufferedImage[] images = new BufferedImage[imageFiles.length];
} // end of method loadImages(File directory)
public Container getPanel()
{
// Hands execution back to the mainPanel function.
return mainPanel;
}// end of method getPanel()
public JMenuBar getMenuBar()
{
// Hands execution back to the menuBar function.
return menuBar;
}// end of method getMenuBar()
public JScrollPane getScrollBar()
{
// Hands execution back to the menuBar function.
return scrollBar;
}// end of method getScrollBar()
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// Input all the compoenents of the photo viewer to the JFrame to display them.
PhotoViewer imageList = new PhotoViewer();
// Creates a new JFrame to display everything.
JFrame mainFrame = new JFrame("Photo Viewer");
// 'Throws away' the JFrame on close.
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Adds all the different components to the JFrame.
mainFrame.add(imageList.getPanel());
mainFrame.add(imageList.getScrollBar());
mainFrame.setJMenuBar(imageList.getMenuBar());
mainFrame.setLocationByPlatform(true);
// Packs all the components into the JFrame.
mainFrame.pack();
// Sets the size of the JFrame.
mainFrame.setSize(1500,1500);
// Allows us to see the JFrame.
mainFrame.setVisible(true);
}
});
} // end of method main(String[] args)
} // end of class PhotoViewer
There is no need to add mainPanel and scrollBar separately, as scrollBar already contains mainPanel. Just execute mainFrame.add(imageList.getScrollBar()); and don't call mainFrame.add(imageList.getPanel()); at all. A single control can be added only to one container.
Default layout of JFrame is BorderLayout. When you add controls to BorderLayout without specifying layout constraint it places the control in BorderLayout.CENTER, effectively replacing whatever there was before.
Just a minor change to your code :)
instead of
scrollBar = new JScrollPane(mainPanel);
use
scrollBar = new JScrollPane(imageView);
mainPanel.add(scrollBar);
and there is no need for
mainFrame.add(imageList.getScrollBar());
I'm trying to create two JLabels - one for icon (UIManager.getIcon("OptionPane.informationIcon") - one of Java's standart icons) and one for text. Sure, I know there is a JLabel's constructor which can make one label from icon and text, but I need exactly two labels, because one of them should be highlighted when mouse moves through it, I omit this part of code.
The problem is I can't find out how to change Icon size. At least I want to set manually height of icon, but it would be better if its height calculated automatically to fit text with specified font. I spent several hours trying to find information in the Web, but couldn't find anything relative.
I tried to implement Icon class and override getIconHeight() and getIconWidth() methods, but I don't know what to do next with my Icon object, because Icon is an interface so it has no constructors.
Here is my simplified code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import net.miginfocom.swing.MigLayout;
public class AppView
{
private final JFrame main_frame;
public AppView()
{
main_frame = new JFrame();
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main_frame.setTitle("Example");
JPanel main_panel = new JPanel() {
private static final long serialVersionUID = 1L;
public Dimension getPreferredSize()
{
return new Dimension(200, 200);
}
};
main_frame.getContentPane().add(main_panel, BorderLayout.CENTER);
main_panel.setLayout(new MigLayout());
JLabel label_icon = new JLabel(UIManager.getIcon("OptionPane.informationIcon"));
JLabel label_text = new JLabel("Text goes here");
label_text.setFont(new Font("sans-serif", Font.PLAIN, 12));
main_panel.add(label_icon);
main_panel.add(label_text);
main_frame.pack();
main_frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new AppView();
}
});
}
}
Here is the result, as you can see, Icon is higher than text:
Thanks in advance!
I'm facing a problem when applying a native Look & Feel to my JFrame, all text (except HTML formatted JLabel) have an ugly bold font.
The very simple UI in the following code sum up all the differences I can see with this L&F :
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
public class HelloWorld extends JFrame {
public HelloWorld() {
Box b = Box.createVerticalBox();
// Labels
JLabel bold = new JLabel("I'm bold !");
JLabel notBold = new JLabel("<html><em>I'm not bold !</em></html>");
b.add(bold);
b.add(notBold);
// Scrollbars example
JPanel scrollViewPort = new JPanel();
scrollViewPort.setPreferredSize(new Dimension(400, 400));
JScrollPane scroll = new JScrollPane(scrollViewPort);
b.add(scroll);
add(b, BorderLayout.CENTER);
// Bold menu
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Menu");
JMenuItem item = new JMenuItem("Item");
menu.add(item);
menubar.add(menu);
setJMenuBar(menubar);
setPreferredSize(new Dimension(200, 200));
pack();
}
public static void setNativeLAF() {
// Native L&F
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Unable to set native look and feel: " + e);
}
}
public static void main(String[] args) {
// setNativeLAF();
HelloWorld app = new HelloWorld();
app.setVisible(true);
}
}
See the difference :
with native L&F (GTK+)
with Metal L&F
by commenting out the setNativeLAF() call.
I'm applying the native look and feel right when my application starts, before any window appears. The UIManager gives me GTK+ (com.sun.java.swing.plaf.gtk.GTKLookAndFeel) as the native look and feel, which is ok since i'm using a Gnome 3 desktop.
I have three problems with this right now :
The GTK+ look and feel doesn't looks like the GTK theme (see the scrollbars)
The JMenuBar seems disabled (not the case with the Metal L&F)
The font is bold ! (same problem with Metal L&F, but fixable)
Any help or explanation about why the GTK+ L&F does that on my system whould be appreciated.
Edit: Here is what a "classic" application looks like on my system in eclipse.
I see several things worth mentioning:
You can apply a derived font to a label, as shown below.
The relevant specification for HTML in a component sys "EM basic emphasis typically rendered in an italic font".
See also Initial Threads.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class HelloWorld extends JFrame {
public HelloWorld() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Box b = Box.createVerticalBox();
// Labels
JLabel bold = new JLabel("I'm bold !");
bold.setFont(bold.getFont().deriveFont(Font.BOLD));
JLabel notBold = new JLabel("<html><em>I'm not bold !</em></html>");
b.add(bold);
b.add(notBold);
// Scrollbars example
JPanel scrollViewPort = new JPanel();
scrollViewPort.setPreferredSize(new Dimension(400, 200));
JScrollPane scroll = new JScrollPane(scrollViewPort);
b.add(scroll);
add(b, BorderLayout.CENTER);
// Bold menu
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Menu");
JMenuItem item = new JMenuItem("Item");
menu.add(item);
menubar.add(menu);
setJMenuBar(menubar);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
HelloWorld app = new HelloWorld();
app.setVisible(true);
}
});
}
}