我以前尝试过这个并且失败了。 如何将JButton添加到JFrame中,以便将其恢复到之前打开的JFrame或“Home”框架。
在我的应用程序中,我有4个JFrame,主要一个和3个按钮可访问。
我的完整代码(Quiet Long,抱歉)
package me.jamplifier;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.DefaultCaret;
public class Launcher extends JFrame {
// Main Class Area (Default/Main Windows)
public Launcher(){
// Main Window
JFrame frame = new JFrame("HC Navigator");
frame.setSize(250, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Text Labels
JLabel label = new JLabel("Beta 1.5 ");
JLabel label1 = new JLabel("Welcome to HurricaneCraft");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label);
panel.add(label1);
panel.setBackground(Color.CYAN);
Font font = new Font("Verdana", Font.BOLD, 12);
label1.setFont(font);
label1.setForeground(Color.RED);
label.setFont(font);
label.setForeground(Color.GRAY);
// Buttons
JButton site = new JButton("Site");
site.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://www.hurricanecraft.com");
}
});
JButton forums = new JButton("Forums");
forums.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://www.hurricanecraft.com/forums/");
}
});
JButton apps = new JButton("Member Apps");
apps.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://hurricanecraft.freeforums.org/member-registration-f29.html");
}
});
JButton community = new JButton("Community");
community.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
createFrame2();
}
});
JButton admin = new JButton("Admin Area");
admin.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
createFrame();
}
});
JButton update = new JButton("Update Log");
update.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
createFrame1();
}
});
panel.add(site);
panel.add(forums);
panel.add(community);
panel.add(apps);
panel.add(admin);
panel.add(update);
}
// Web Browser/Link Opening Code
public static void openWebPage(String url){
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
// Admin Area Panel
public static void createFrame()
{
JFrame frame = new JFrame("Admin Area");
frame.setVisible(true);
frame.setSize(250, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Beta 1.5 ");
JLabel label1 = new JLabel("Welcome to the Admin Panel");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label);
panel.add(label1);
JButton host = new JButton("MCServer Host");
host.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://panel.fadehost.com/index");
}
});
JButton console = new JButton("MCServer Console");
console.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://panel.fadehost.com/index.php?r=server/log&id=1983");
}
});
JButton tfhost = new JButton("TF2Server Host");
host.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://tcadmin.darkstarllc.com");
}
});
JButton staffforum = new JButton("Staff Forums");
staffforum.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://hurricanecraft.freeforums.org/staff-area-f39.html");
}
});
panel.add(host);
panel.add(console);
panel.add(tfhost);
panel.add(staffforum);
}
// Update Logs Panel
public static void createFrame1()
{
JFrame frame = new JFrame("Update Logs");
frame.setVisible(true);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel(" Change Log: ");
JLabel label1 = new JLabel("- Beta 1.1: First Release");
JLabel label2 = new JLabel("- Beta 1.2: Added ADMIN Panel & Change log ");
JLabel label3 = new JLabel("- Beta 1.3: Added Staff Forum & Seperated Console/Host");
JLabel label4 = new JLabel("- Beta 1.4: Added Colours & Community Tab + Online Status's");
JLabel label5 = new JLabel("- Beta 1.4: Added Colours & Community Tab + Online Status's");
JLabel label6 = new JLabel("- Beta 1.5: Added New Site Redirects & TF2 Server IP/PANEL");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label);
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5);
panel.add(label6);
}
// Community Page
public static void createFrame2()
{
JFrame frame = new JFrame("Community Area");
frame.setVisible(true);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Labels Frames Panels ECT
JLabel label = new JLabel("Hurricane Craft Servers: ");
JLabel minecraft = new JLabel("- Minecraft: IP Mc.hurricanecraft.com");
JLabel online = new JLabel("[ONLINE]");
JLabel tf2 = new JLabel("- Teamspeak: IP tf.hurricanecraft.com:27015");
JLabel online1 = new JLabel("[ONLINE]");
JLabel teamspeak = new JLabel("- Teamfortress: IP ts.hurricanecraft.com:5326");
JLabel online2 = new JLabel("[ONLINE]");
JLabel site = new JLabel("- Site: http://www.hurricanegaming.webs.com");
JLabel online3 = new JLabel("[ONLINE]");
JPanel panel = new JPanel();
// Buttons
JButton votepmc = new JButton("Vote On PMC");
votepmc.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://www.planetminecraft.com/server/hurricane-craft-247-survival-
creative-pvp-games/vote/");
}
});
JButton votemc = new JButton("Vote On MCServers");
votemc.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://minecraftservers.org/vote/97507");
}
});
JButton votetopg = new JButton("Vote On Topg");
votetopg.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openWebPage("http://topg.org/Minecraft/in-371684");
}
});
// Adding buttons/labels
frame.add(panel);
panel.add(label);
panel.add(minecraft);
panel.add(online);
panel.add(tf2);
panel.add(online1);
panel.add(teamspeak);
panel.add(online2);
panel.add(site);;
panel.add(online3);
panel.add(votepmc);
panel.add(votemc);
panel.add(votetopg);
// Font & Colours
Font font = new Font("Verdana", Font.BOLD, 12);
label.setFont(font);
label.setForeground(Color.BLUE);
Font green = new Font("Verdana", Font.BOLD, 12);
online.setFont(green);
online.setForeground(Color.GREEN);
online1.setFont(green);
online1.setForeground(Color.GREEN);
online2.setFont(green);
online2.setForeground(Color.GREEN);
online3.setFont(green);
online3.setForeground(Color.GREEN);
}
// Main String Args Declaration
public static void main(String[] args){
new Launcher();
}
}
答案 0 :(得分:3)
如何使用一个布局CardLayout
和四个JPanel
来处理内容。它有三个很好的功能:它将它的布局组件视为卡片:
CardLayout.first(Caontainer)
:翻转到容器的第一张卡。CardLayout.next(panel)
:翻转到指定容器的下一张卡。如果当前可见的卡片是最后一张,则此方法将翻转到布局中的第一张卡片。CardLayout.previous(paenl)
:翻转到指定容器的上一个卡。如果当前可见的卡片是第一张,则此方法将翻转到布局中的最后一张卡片。以下是您的工作示例:
class CardLayoutDemo1 extends JFrame {
private JPanel jPanel1;
private JButton navHomeButt;
private JButton navNextButt;
private JButton navPreviousButt;
private JPanel panelContainer;
Random random = new Random();
public CardLayoutDemo1() {
initComponents();
panelContainer.add(createSamplePanel("Home Panel "), ""+0);
for(int i=1; i < 10; i++)
{
panelContainer.add(createSamplePanel("Panel "+i), ""+i);
}
}
private void initComponents() {
jPanel1 = new JPanel();
navPreviousButt = new JButton();
navNextButt = new JButton();
navHomeButt = new JButton();
panelContainer = new JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(Color.WHITE);
navPreviousButt.setText("Previous");
navPreviousButt.setPreferredSize(new Dimension(90, 23));
jPanel1.add(navPreviousButt);
navNextButt.setText("next");
navNextButt.setPreferredSize(new Dimension(90, 23));
jPanel1.add(navNextButt);
navHomeButt.setText("Back to Home");
jPanel1.add(navHomeButt);
panelContainer.setPreferredSize(new Dimension(400, 300));
panelContainer.setLayout(new CardLayout());
// setting the card layout
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
getContentPane().add(panelContainer, BorderLayout.CENTER);
navNextButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
cardLayout.next(panelContainer);
// using cardLayout next() to go to next panel
}
});
navHomeButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
cardLayout.first(panelContainer);
// suing first to get to the home panel
}
});
navPreviousButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
CardLayout cardLayout = (CardLayout) panelContainer.getLayout();
cardLayout.previous(panelContainer);
// using previous to get to previous(left)panel
}
});
pack();
}
public JPanel createSamplePanel(String panelTitle)
{
JPanel samplePanel = new JPanel();
samplePanel.add(new JLabel(panelTitle));
samplePanel.setBackground(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
return samplePanel;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CardLayoutDemo1().setVisible(true);
}
});
}
}