簡體   English   中英

從jframe調用japplet

[英]Call japplet from jframe

manually設計了JApplet類,我使用Netbean Builder設計了主類,默認情況下會自動生成代碼,類將擴展JFrame。 那我怎么能從JFrame運行JApplet類。
這個JApplet代碼
the problem now is buttons does not appear
更新

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 viewer)的上下文中運行,它們實際上並不是設計用於添加到其他容器中。

從技術上講,你可以像任何其他組件一樣將小程序添加到框架中,但就個人而言,我不會。 applet希望能夠獲得更多信息,以便它能夠完全運行。

相反,我會將所有“應用程序”內容移動到一個單獨的組件,例如JPanel ,只需根據需要在applet或框架之間移動它...

ps-您可以使用f.setLocationRelativeTo(null)將窗口置於屏幕中心;)

更新

你需要回到基礎。 除非你絕對必須擁有一個,否則在你理解Swing的基礎知識之前要避免使用applets,這是一個很好的例子......

GalzyTable2的構造函數中,你正在做......

JApplet app = new JApplet();
add(app);
app.init();
app.start();

...為什么要向applet添加另一個applet?

舉個例子......

main方法中,您嘗試將JFrame的實例添加到自身...

f.getContentPane().add(f, button2);

相反,創建一個從JPanel擴展的類,將UI邏輯添加到此,如果需要,使用復合組件。

然后,將此面板添加到您需要的任何頂級容器中。

花點時間閱讀使用Swing創建GUI

更新了示例

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

您似乎也對如何使用布局管理器缺乏了解。

花點時間閱讀通過在容器中 創建具有SwingLaying組件 的GUI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM