I'm attempting to horizontally center a JLabel that contains inline HTML, but I can't seem to get it to work. I've tried manually centering it, but had no luck.
Here's what it looks like for me:
Full class code:
import java.awt.Button;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class Client extends JFrame{
private final String url = "http://example.com/";
public Client() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 225);
setResizable(false);
setLocationRelativeTo(null);
addComponents(mainPanel);
add(mainPanel);
setTitle(null);
setVisible(true);
}
public void addComponents(JPanel pane) {
Button btn1 = new Button("1");
btn1.setSize(200, 50);
btn1.setFocusable(false);
pane.add(btn1);
Button btn2 = new Button("2");
btn2.setSize(200, 50);
btn2.setFocusable(false);
pane.add(btn2);
Button btn3 = new Button("3");
btn3.setSize(200, 50);
btn3.setFocusable(false);
pane.add(btn3);
JLabel lblWebsite = new JLabel("<html>Visit lblAbout1</html>", BoxLayout.X_AXIS);
lblWebsite.setCursor(new Cursor(Cursor.HAND_CURSOR));
lblWebsite.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
try {
Desktop.getDesktop().browse(new URI(url));
} catch (IOException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
}
});
pane.add(lblWebsite);
}
}
Try with JComponent#setAlignmentX() method
Enclose the anchor inside a div that is aligned to center.
JLabel lblWebsite = new JLabel("<html><div align='center'>Visit lblAbout1</div></html>", BoxLayout.X_AXIS);
lblWebsite.setAlignmentX(Component.CENTER_ALIGNMENT);
Snapshots:
Related
Can someone help me with my code, please. I am expiriencing strange behaviour in Eclipse.
Sometimes my application loads correctly and most of times I don't get North and South JPanels loaded properly or not loaded at all.
After loading GUI displays correctly only when I resize JFrame borders, but I would like to load every time correctly. I guess I have some trouble in my code but I can't find it. And btw, I am running Linux Mint 19.1 Tarra with java-11-openjdk-amd64.
Here is code for Main class:
package gui;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
new Dakijevstina();
}
}
And here is GUI class code:
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
#SuppressWarnings("serial")
public class Dakijevstina extends JFrame{
Font font = new Font("TimesNew Roman", Font.PLAIN, 12);
public Dakijevstina() {
initComponents();
}
public void initComponents() {
//setting up JFrame
setTitle("Market garden financial software");
setFont(font);
setLayout(new BorderLayout());
setSize(640, 480);
setDefaultCloseOperation(Dakijevstina.EXIT_ON_CLOSE);
setLocationByPlatform(true);
setVisible(true);
//setting up North JPanel
JPanel pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlNorth.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
add(pnlNorth, BorderLayout.NORTH);
//setting up StatusBar
JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlSouth.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
add(pnlSouth, BorderLayout.SOUTH);
//adding buttons to north JPanel
//About
JButton btnAbout = new JButton();
JButton btnExit = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("/images/about.png"));
Image img2 = ImageIO.read(getClass().getResource("/images/exit.png"));
btnAbout.setIcon(new ImageIcon(img));
btnExit.setIcon(new ImageIcon(img2));
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex);
}
btnAbout.setPreferredSize(new Dimension(40, 40));
btnAbout.setMinimumSize(new Dimension(40, 40));
btnAbout.setToolTipText("Information about author");
btnExit.setPreferredSize(new Dimension(40, 40));
btnExit.setMinimumSize(new Dimension(40, 40));
btnExit.setToolTipText("Exit application");
btnExit.addActionListener(e -> exitApp());
pnlNorth.add(btnAbout);
pnlNorth.add(btnExit);
//adding StatusBar to south JPanel
JLabel status = new JLabel("Welcome to Dakijevstina software");
status.setFont(font);
pnlSouth.add(status);
}
//event handlers
public void exitApp() {
this.dispose();
}
This is what I get most of time:
unwanted behaviour
And this is how it supose to be:
this is what I want
I have a JFrame, that contains a JPanel, which contains a JScrollPane, that contains another JPanel, with two components (JPanels).
For some reason, when I use WindowBuilder's preview option to see the frame, the JScrollPane shows the horizontal scroll bar, but when I compile and run the app, it doesn't.
Here is what it looks like:
from preview option:
When it's compiled:
Here is my code:
package home;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.SystemColor;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Font;
import javax.swing.BoxLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.ScrollPaneConstants;
public class AtmManeger implements Serializable {
private JFrame frmAtmManeger;
public int NumOfOpenAtmMachines = 0;
private final AtmManeger frame = this;
private ArrayList<ATMmachine> ATMs = new ArrayList<ATMmachine>();//Array list of all the ATM machines that were opened
private AtmAccountDataBase atmDataBase = new AtmAccountDataBase();
private JLabel contLabel = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AtmManeger window = new AtmManeger();
window.frmAtmManeger.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AtmManeger getFrame() {
return this.frame;
}
public AtmManeger() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frmAtmManeger = new JFrame();
frmAtmManeger.setMinimumSize(new Dimension(615, 420));
frmAtmManeger.getContentPane().setBackground(SystemColor.activeCaption);
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(10, 10));
frmAtmManeger.setContentPane(mainPanel);
JScrollPane scrollPane = new JScrollPane();
mainPanel.add(scrollPane, BorderLayout.CENTER);
JPanel subPanel = new JPanel();
subPanel.setSize(new Dimension(1190, 350));
subPanel.setBackground(SystemColor.activeCaption);
subPanel.setPreferredSize(new Dimension(1190, 350));
scrollPane.setViewportView(subPanel);
subPanel.setLayout(new MigLayout("", "[50.00%,grow][50.00%,grow]", "[grow]"));
JPanel panel = new JPanel();
subPanel.add(panel,"cell 0 0,grow");
panel.setBorder(null);
panel.setBackground(SystemColor.textHighlight);
panel.setLayout(new MigLayout("", "[100.00%,grow]", "[71px][13.54%][10.09%][41px][][grow]"));
JPanel panel_1 = new JPanel();
subPanel.add(panel_1, "cell 1 0,grow");
frmAtmManeger.setTitle("ATM Maneger");
frmAtmManeger.setBounds(700, 400, 609, 420);
frmAtmManeger.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I made another app very similar to this one, only that one inherents JFrame. I couldn't find any difference between this code and the one above. code:
package home;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.FlowLayout;
import net.miginfocom.swing.MigLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JButton;
public class test02 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test02 frame = new test02();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public test02() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// Handle exception
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 432, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(800, 220));
panel.setBackground(Color.RED);
scrollPane.setViewportView(panel);
panel.setLayout(new MigLayout("", "[50.00%,grow][50.00%,grow]", "[grow]"));
JPanel panel_1 = new JPanel();
panel.add(panel_1, "cell 0 0,grow");
panel_1.setLayout(new MigLayout("", "[][][][][][][]", "[][][][][][]"));
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel, "cell 2 1");
JButton btnNewButton_1 = new JButton("New button");
panel_1.add(btnNewButton_1, "cell 3 3");
JButton btnNewButton = new JButton("New button");
panel_1.add(btnNewButton, "cell 2 5");
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1, "cell 6 5");
JPanel panel_2 = new JPanel();
panel.add(panel_2, "cell 1 0,grow");
}
}
So it's probably just a silly mistake, but what is it?
I don't see you changing the scrollbar policy.
Since there is no overflow, without changing that setting, you will not see the scrollbar appear.
try:
scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
I have written a program to display panels as per the requirements.
Code:
private void createUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JScrollPane pane = new JScrollPane();
JPanel leftPnl = new JPanel(new BorderLayout());
JPanel rightPnl = new JPanel(new BorderLayout());
leftPnl.add(new JLabel("Left"), BorderLayout.CENTER);
leftPnl.setBorder(new LineBorder(Color.black, 5));
leftPnl.setPreferredSize(new Dimension(400, 400));
rightPnl.add(new JLabel("Right"), BorderLayout.CENTER);
rightPnl.setBorder(new LineBorder(Color.black, 5));
rightPnl.setPreferredSize(new Dimension(400, 400));
panel.add(leftPnl);
panel.add(rightPnl);
panel.add(pane);
frame.setPreferredSize(new Dimension(400, 400));
frame.setTitle("ScrollPane Example");
frame.add(new JScrollPane(panel));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I have a JCheckBox with some text and a JLabel with an ImageIcon
Requirement is by checking the JCheckBox, the JCheckBox text and JLabel icon should be displayed in JEditorPane
I used
if (jCheckBox3.isSelected()) {
s1 = jCheckBox3.getText() + jLabel1.getIcon() ;
}
And
jEditorPane1.setText(s1);
output is
“checkbox text” file:/G:/myProject/build/classes/myproject/img.png
Instead of image I am getting image path
This is an example how you could do it:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CheckBoxCheckingDisplayImagePanel extends JPanel implements ActionListener {
private JCheckBox checkBox;
private JLabel label;
private JEditorPane editorPane;
private JPanel editorPanel;
public CheckBoxCheckingDisplayImagePanel() {
try {
JPanel panel = new JPanel(new GridLayout(0, 2));
this.setLayout(new BorderLayout(10, 10));
this.add(panel,BorderLayout.NORTH);
ImageIcon icon = new ImageIcon(ImageIO.read(new URL("http://i.stack.imgur.com/lxthA.jpg")));
label = new JLabel(icon, SwingConstants.HORIZONTAL);
checkBox = new JCheckBox("Check me!");
checkBox.addActionListener(this);
panel.add(label);
panel.add(checkBox);
editorPane = new JEditorPane();
this.add(editorPane,BorderLayout.CENTER);
} catch (MalformedURLException ex) {
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CheckBoxCheckingDisplayImagePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(checkBox)) {
editorPane.setContentType("text/html");
editorPane.setText("<img src=\"http://i.stack.imgur.com/lxthA.jpg\">");
}
}
}
Patrick
Using pure java, I would like to have a player press a JButton, have a text box pop up which they can type in, and then have a certain action happen when the player presses "Enter".
How could I do this?
I have not yet attempted this because I do not know where to start, however my current code is
package Joehot200;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main extends JFrame {
static boolean start = false;
private JPanel contentPane;
/**
* Launch the application.
*/
static Main frame = null;
String version = "0.4";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new Main();
frame.setVisible(true);
frame.setTitle("Privateers");
} catch (Exception e) {
e.printStackTrace();
}
}
});
TerrainDemo.startGame();
}
boolean cantConnect = false;
/**
* Create the frame.
*/
public Main() {
setExtendedState(JFrame.MAXIMIZED_BOTH);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
final JButton btnEnterBattlefield = new JButton("Enter battlefield!");
btnEnterBattlefield.setForeground(Color.red);
//btnEnterBattlefield.setBackground(Color.green);
//btnEnterBattlefield.setOpaque(true);
menuBar.add(btnEnterBattlefield);
btnEnterBattlefield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println(new File(System.getProperty("user.dir") + "res/images/heightmap.bmp").getAbsolutePath());
//System.out.println("You clicked the button");
if (cantConnect){
btnEnterBattlefield.setText("Unable to connect to the server!");
}else{
btnEnterBattlefield.setText("Connecting to server...");
}
start = true;
}
});
//JMenu mnLogIn = new JMenu("Log in");
JMenu mnLogIn = new JMenu("Currently useless button");
mnLogIn.setForeground(Color.magenta);
menuBar.add(mnLogIn);
JButton btnLogIntoGame = new JButton("Log into game.");
mnLogIn.add(btnLogIntoGame);
JButton btnRegisterNewAccount = new JButton("Register new account");
mnLogIn.add(btnRegisterNewAccount);
final JButton btnGoToWebsite = new JButton("We currently do not but soon will have a website.");
btnGoToWebsite.setForeground(Color.BLUE);
//btnGoToWebsite = new JButton("Go to website.");
btnGoToWebsite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("Going to website!");
try {
java.awt.Desktop.getDesktop().browse(new URI("www.endcraft.net/none"));
} catch (Exception e1) {
btnGoToWebsite.setText("Error going to website!");
}
}
});
menuBar.add(btnGoToWebsite);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
FlowLayout flowLayout = (FlowLayout) contentPane.getLayout();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "/res/background.png");
Image img = icon.getImage() ;
Image newimg = img.getScaledInstance(this.getWidth()*3, this.getHeight()*3, java.awt.Image.SCALE_SMOOTH ) ;
icon = new ImageIcon( newimg );
JLabel background=new JLabel(icon);
getContentPane().add(background);
background.setLayout(new FlowLayout());
//JProgressBar progressBar = new JProgressBar();
//contentPane.add(progressBar);
//JMenu mnWelcomeToA = new JMenu("Welcome to a pirate game!");
//contentPane.add(mnWelcomeToA);
//JButton btnStartGame = new JButton("Start game");
//mnWelcomeToA.add(btnStartGame);
//JSplitPane splitPane = new JSplitPane();
//mnWelcomeToA.add(splitPane);
//JButton btnRegister = new JButton("Register");
//splitPane.setLeftComponent(btnRegister);
//JButton btnLogin = new JButton("Login");
//splitPane.setRightComponent(btnLogin);
}
}
It is the "Register account" and "Log into game" button that I would like to have the above described action happen on.
Would be great if someone could tell me how to do this.
If you decide to implement a new frame (not best practice), with a new panel, and the JTextField or JTextArea within, you must bind a listener to the button that calls setVisible() on the newly created frame..
For example:
public void actionPerformed(ActionEvent e) {
yourFrame.setVisible(true);
}
Further, you could create the frame everytime the button is clicked, within the actionPerformed method aswell, however this is also not best practice..
A better alternative may be to implement a JOptionPane, see here or here..
I'm creating an about JFrame for my program. I have an icon which I used for the program and I have that show up as the first thing on the about JFrame, but I'm having issues trying to center the image. If I do some kind of centering it screws up the whole alignment of everything else.
I'm trying to have all the JLabels, other than the icon, to be left aligned. Then have the icon aligned to the center.
I had to remove some personal information, whatever I did remove I put them between "[]".
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class About extends JFrame {
public About() {
super("About [PROGRAM]");
setIconImage([PROGRAM].getInstance().setIcon());
JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
main.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JLabel icon = new JLabel("", new ImageIcon(getClass().getResource(Constants.ICON_FULL)), JLabel.CENTER);
JLabel name = new JLabel("[PROGRAM]");
JLabel expandedName = new JLabel("[PROGRAM DESCRIPTION]");
JLabel copyright = new JLabel("[COPYRIGHT JUNK]");
JLabel credits = new JLabel("[CREDITS]");
name.setFont(new Font(name.getFont().getFamily(), Font.BOLD, 18));
copyright.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
main.add(icon);
main.add(Box.createRigidArea(new Dimension(0, 10)));
main.add(name);
main.add(expandedName);
main.add(copyright);
main.add(credits);
add(main);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
Consider using some layouts to help you out. Ones that come to mind include BorderLayout with the icon in the BorderLayout.CENTER position. You can stack stuff on one side using a BoxLayout using JPanel that is added to the main BorderLayout-using JPanel.
e.g.,
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class About extends JDialog {
public static final String IMAGE_PATH = "http://upload.wikimedia.org/wikipedia/"
+ "commons/thumb/3/39/European_Common_Frog_Rana_temporaria.jpg/"
+ "800px-European_Common_Frog_Rana_temporaria.jpg";
public About(JFrame frame) {
super(frame, "About [PROGRAM]", true);
ImageIcon myIcon = null;
try {
URL imgUrl = new URL(IMAGE_PATH);
BufferedImage img = ImageIO.read(imgUrl);
myIcon = new ImageIcon(img);
} catch (MalformedURLException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
JPanel main = new JPanel(new BorderLayout());
main.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel centerLabel = new JLabel(myIcon);
JLabel name = new JLabel("[PROGRAM]");
JLabel expandedName = new JLabel("[PROGRAM DESCRIPTION]");
JLabel copyright = new JLabel("[COPYRIGHT JUNK]");
JLabel credits = new JLabel("[CREDITS]");
name.setFont(new Font(name.getFont().getFamily(), Font.BOLD, 18));
copyright.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
int eb = 20;
centerLabel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.add(name);
leftPanel.add(Box.createVerticalGlue());
leftPanel.add(expandedName);
leftPanel.add(copyright);
leftPanel.add(credits);
leftPanel.add(Box.createVerticalGlue());
main.add(centerLabel, BorderLayout.CENTER);
main.add(leftPanel, BorderLayout.LINE_START);
add(main);
pack();
}
public static void main(String[] args) {
final JFrame frame = new JFrame("GUI");
JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("About") {
#Override
public void actionPerformed(ActionEvent e) {
About about = new About(frame);
about.setLocationRelativeTo(frame);
about.setVisible(true);
}
}));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}