Java 从jframe调用japplet

Java 从jframe调用japplet,java,swing,jbutton,japplet,Java,Swing,Jbutton,Japplet,我手动设计了JApplet类,,并使用Netbean Builder设计了主类,默认情况下,该类将自动生成代码。那么如何从JFrame运行JApplet类。 此JApplet代码 现在的问题是按钮没有出现 已更新 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import javax.swing.ImageIc

我手动设计了JApplet类
,并使用
Netbean Builder
设计了主类,默认情况下,该类将自动生成代码。那么如何从JFrame运行JApplet类。
此JApplet代码
现在的问题是按钮没有出现

已更新

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class GalaxyTable2 extends JFrame{ //JApplet

    private static final int PREF_W = 700;
    private static final int PREF_H = 600;
    String[] columnNames
                    = {"Phone Name", "Brief Description", "Picture", "price",
                        "Buy"};

// Create image icons
    ImageIcon Image1 = new ImageIcon(
                    getClass().getResource("s1.png"));
    ImageIcon Image2 = new ImageIcon(
                    getClass().getResource("s2.png"));
    ImageIcon Image3 = new ImageIcon(
                    getClass().getResource("s3.png"));
    ImageIcon Image4 = new ImageIcon(
                    getClass().getResource("s4.png"));
    ImageIcon Image5 = new ImageIcon(
                    getClass().getResource("note.png"));
    ImageIcon Image6 = new ImageIcon(
                    getClass().getResource("note2.png"));
    ImageIcon Image7 = new ImageIcon(
                    getClass().getResource("note3.png"));

    Object[][] rowData = {
        {"Galaxy S", "3G Support,CPU 1GHz",
            Image1, 120, false},
        {"Galaxy S II", "3G Support,CPU 1.2GHz",
            Image2, 170, false},
        {"Galaxy S III", "3G Support,CPU 1.4GHz",
            Image3, 205, false},
        {"Galaxy S4", "4G Support,CPU 1.6GHz",
            Image4, 230, false},
        {"Galaxy Note", "4G Support,CPU 1.4GHz",
            Image5, 190, false},
        {"Galaxy Note2 II", "4G Support,CPU 1.6GHz",
            Image6, 190, false},
        {"Galaxy Note 3", "4G Support,CPU 2.3GHz",
            Image7, 260, false},};

    MyTable ss = new MyTable(
                    rowData, columnNames);

    // Create a table
    JTable jTable1 = new JTable(ss);

    public GalaxyTable2() {
    jTable1.setRowHeight(70);

    add(new JScrollPane(jTable1),
   BorderLayout.CENTER);
   JFrame f = new JFrame();

   this.setTitle("Galaxy Phones");
   JButton button = new JButton("Home");
  button.setSize(new Dimension(200, 500));
  button.setLocation(400, 500);
  f.getContentPane().add(button);
  JButton button2 = new JButton("Confirm");
  button2.setSize(new Dimension(100, 500));
  button2.setLocation(100, 300);
  // button2.addActionListener(null);
  f.getContentPane().add(button2);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   this.setSize(800, 700);
  this.setLocation(300, 0);
  }
    @Override

    public Dimension getPreferredSize() {
        return new Dimension(PREF_W, PREF_H);
    }

    public void actionPerformed(ActionEvent e) {
        new AMainFrame7().setVisible(true);
    }

    public static void main(String[] args) {

         GalaxyTable2 b=new GalaxyTable2();
        /*
        JFrame f = new JFrame();

        JButton button = new JButton("Home");
        button.setSize(new Dimension(100, 50));
        button.setLocation(400, 500);
        f.getContentPane().add(button);
        JButton button2 = new JButton("Confirm");
        button2.setSize(new Dimension(100, 50));
        button2.setLocation(200, 500);
        button2.addActionListener(null);
        f.getContentPane().add(button2);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("Galaxy Phones");
        f.setSize(500, 500);
        f.getContentPane().add(f, button2);
        applet.init();
        applet.start();*/
        b.pack();

        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        b.setLocation((d.width - b.getSize().width) / 2,
                        (d.height - b.getSize().height) / 2);
        b.setVisible(true);

    }
}
主类中的此操作代码

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
new GalaxyTable().setVisible(true); 
dispose();
 }   

首先,applet设计为在浏览器(或applet查看器)的上下文中运行,而不是真正设计为添加到其他容器中

从技术上讲,您可以像任何其他组件一样将小程序添加到框架中,但就个人而言,我不会。小程序希望有更多信息可供使用,以使其能够充分工作

相反,我会将所有“应用程序”内容移动到一个单独的组件,例如
JPanel
,然后根据需要在applet或框架之间移动它

ps-您可以使用
f.setLocationRelativeTo(null)
在屏幕上居中显示窗口;)

已更新

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class GalaxyTable2 extends JFrame{ //JApplet

    private static final int PREF_W = 700;
    private static final int PREF_H = 600;
    String[] columnNames
                    = {"Phone Name", "Brief Description", "Picture", "price",
                        "Buy"};

// Create image icons
    ImageIcon Image1 = new ImageIcon(
                    getClass().getResource("s1.png"));
    ImageIcon Image2 = new ImageIcon(
                    getClass().getResource("s2.png"));
    ImageIcon Image3 = new ImageIcon(
                    getClass().getResource("s3.png"));
    ImageIcon Image4 = new ImageIcon(
                    getClass().getResource("s4.png"));
    ImageIcon Image5 = new ImageIcon(
                    getClass().getResource("note.png"));
    ImageIcon Image6 = new ImageIcon(
                    getClass().getResource("note2.png"));
    ImageIcon Image7 = new ImageIcon(
                    getClass().getResource("note3.png"));

    Object[][] rowData = {
        {"Galaxy S", "3G Support,CPU 1GHz",
            Image1, 120, false},
        {"Galaxy S II", "3G Support,CPU 1.2GHz",
            Image2, 170, false},
        {"Galaxy S III", "3G Support,CPU 1.4GHz",
            Image3, 205, false},
        {"Galaxy S4", "4G Support,CPU 1.6GHz",
            Image4, 230, false},
        {"Galaxy Note", "4G Support,CPU 1.4GHz",
            Image5, 190, false},
        {"Galaxy Note2 II", "4G Support,CPU 1.6GHz",
            Image6, 190, false},
        {"Galaxy Note 3", "4G Support,CPU 2.3GHz",
            Image7, 260, false},};

    MyTable ss = new MyTable(
                    rowData, columnNames);

    // Create a table
    JTable jTable1 = new JTable(ss);

    public GalaxyTable2() {
    jTable1.setRowHeight(70);

    add(new JScrollPane(jTable1),
   BorderLayout.CENTER);
   JFrame f = new JFrame();

   this.setTitle("Galaxy Phones");
   JButton button = new JButton("Home");
  button.setSize(new Dimension(200, 500));
  button.setLocation(400, 500);
  f.getContentPane().add(button);
  JButton button2 = new JButton("Confirm");
  button2.setSize(new Dimension(100, 500));
  button2.setLocation(100, 300);
  // button2.addActionListener(null);
  f.getContentPane().add(button2);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   this.setSize(800, 700);
  this.setLocation(300, 0);
  }
    @Override

    public Dimension getPreferredSize() {
        return new Dimension(PREF_W, PREF_H);
    }

    public void actionPerformed(ActionEvent e) {
        new AMainFrame7().setVisible(true);
    }

    public static void main(String[] args) {

         GalaxyTable2 b=new GalaxyTable2();
        /*
        JFrame f = new JFrame();

        JButton button = new JButton("Home");
        button.setSize(new Dimension(100, 50));
        button.setLocation(400, 500);
        f.getContentPane().add(button);
        JButton button2 = new JButton("Confirm");
        button2.setSize(new Dimension(100, 50));
        button2.setLocation(200, 500);
        button2.addActionListener(null);
        f.getContentPane().add(button2);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("Galaxy Phones");
        f.setSize(500, 500);
        f.getContentPane().add(f, button2);
        applet.init();
        applet.start();*/
        b.pack();

        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        b.setLocation((d.width - b.getSize().width) / 2,
                        (d.height - b.getSize().height) / 2);
        b.setVisible(true);

    }
}
你需要回到基础上来。除非你绝对必须有一个,否则在你了解Swing的基本知识之前,不要使用小程序,以此类推

GalzyTable2
的构造函数中,您正在执行

JApplet app = new JApplet();
add(app);
app.init();
app.start();
…为什么要将另一个小程序添加到小程序

举个例子

main
方法中,您试图将
JFrame
的实例添加到自身中

f.getContentPane().add(f, button2);
相反,您可以自己创建一个类,该类从类似
JPanel
的东西扩展而来,将您的UI逻辑添加到该类中,如果需要,可以使用复合组件

然后,将此面板添加到所需的顶级容器中

花点时间通读一遍

用示例更新

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GalaxyTable2 extends JPanel {

    private static final int PREF_W = 700;
    private static final int PREF_H = 600;

    String[] columnNames
                    = {"Phone Name", "Brief Description", "Picture", "price",
                        "Buy"};

// Create image icons
    ImageIcon Image1 = new ImageIcon(
                    getClass().getResource("s1.png"));
    ImageIcon Image2 = new ImageIcon(
                    getClass().getResource("s2.png"));
    ImageIcon Image3 = new ImageIcon(
                    getClass().getResource("s3.png"));
    ImageIcon Image4 = new ImageIcon(
                    getClass().getResource("s4.png"));
    ImageIcon Image5 = new ImageIcon(
                    getClass().getResource("note.png"));
    ImageIcon Image6 = new ImageIcon(
                    getClass().getResource("note2.png"));
    ImageIcon Image7 = new ImageIcon(
                    getClass().getResource("note3.png"));

    Object[][] rowData = {
        {"Galaxy S", "3G Support,CPU 1GHz",
            Image1, 120, false},
        {"Galaxy S II", "3G Support,CPU 1.2GHz",
            Image2, 170, false},
        {"Galaxy S III", "3G Support,CPU 1.4GHz",
            Image3, 205, false},
        {"Galaxy S4", "4G Support,CPU 1.6GHz",
            Image4, 230, false},
        {"Galaxy Note", "4G Support,CPU 1.4GHz",
            Image5, 190, false},
        {"Galaxy Note2 II", "4G Support,CPU 1.6GHz",
            Image6, 190, false},
        {"Galaxy Note 3", "4G Support,CPU 2.3GHz",
            Image7, 260, false},};

    MyTable ss = new MyTable(
                    rowData, columnNames);

    // Create a table
    JTable jTable1 = new JTable(ss);

    public GalaxyTable2() {
        jTable1.setRowHeight(70);

        add(new JScrollPane(jTable1),
                        BorderLayout.CENTER);

        JPanel buttons = new JPanel();

        JButton button = new JButton("Home");
        buttons.add(button);
        JButton button2 = new JButton("Confirm");
        buttons.add(button2);

        add(buttons, BorderLayout.SOUTH);
    }

    @Override

    public Dimension getPreferredSize() {
        return new Dimension(PREF_W, PREF_H);
    }

    public void actionPerformed(ActionEvent e) {
        new AMainFrame7().setVisible(true);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new GalaxyTable2());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
您似乎还不了解如何使用布局管理器


花点时间通读一遍,然后

嗯,您确定要在创建小程序后调用
dispose()
?另外,您没有将小程序添加到任何内容中…我使用了
dispose
在打开JApplet类(即GalaxyTable)之后关闭main类。我现在能做什么?我使用了
f.setLocationRelativeTo(null)问题仍然存在。当我调用“无窗口显示”时。@lovingjava您需要将小程序添加到框架中,并且至少不要关闭框架。这不是我的观点,我的观点是,您首先不应该尝试这样做……我编辑过,现在工作正常,但仍然存在一个问题,即按钮没有显示。请参阅更新的
代码。在
GalaxyTable2
的构造函数中,您创建了第二个帧,但是
GalaxyTable2
已经从
JFrame
扩展…使
GalaxyTable2
JPanel
扩展,在
main
方法中,创建
JFrame的实例并将您的面板添加到其中。。。