返回/主页按钮 [Java] [英] A Back/Home Button [Java]

查看:50
本文介绍了返回/主页按钮 [Java]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前自己尝试过,但失败了.如何将 JButton 添加到 JFrame 以将其带回之前打开的 JFrame 或主页"框架.

Ive attempted this before on my own and failed. How can I add a JButton to a JFrame that will bring it back to the previous opened JFrame or the "Home" Frame.

在我的应用程序中,我有 4 个 JFrame,主要的和 3 个可通过按钮访问的.

In my application I have 4 JFrames, The Main One and 3 Accessible by Buttons.

我的完整代码(安静,抱歉)

My Full Code(Quiet Long, sorry)

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


    }

推荐答案

如何使用一种布局,CardLayout 和四个 JPanel 与内容一起使用.它有三个不错的功能: 把它的布局组件看成卡片:

How about using one layout, CardLayout and four JPanel with content to work with. It has three nice function: It sees it's lay-outing component as card:

  1. CardLayout.first(Caontainer):翻转到容器的第一张卡片.
  2. CardLayout.next(panel):翻转到指定容器的下一张卡片.如果当前可见的卡片是最后一张,则此方法会翻转到布局中的第一张卡片.
  3. CardLayout.previous(paenl):翻转到指定容器的上一个卡片.如果当前可见的卡片是第一张卡片,则此方法会翻转到布局中的最后一张卡片.
  1. CardLayout.first(Caontainer): Flips to the first card of the container.
  2. CardLayout.next(panel): Flips to the next card of the specified container. If the currently visible card is the last one, this method flips to the first card in the layout.
  3. CardLayout.previous(paenl): Flips to the previous card of the specified container. If the currently visible card is the first one, this method flips to the last card in the layout.

这是一个工作示例:

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

}

这篇关于返回/主页按钮 [Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆