Java 在小程序查看器中调用时出现空白屏幕
这是我第一次在这里发帖。 我在做一些家庭作业时,第一次在java中使用GUI。我已经开始逐步编写一份意大利餐厅菜单 下面的代码编译良好,没有错误。编译后,我运行applet viewerJava 在小程序查看器中调用时出现空白屏幕,java,swing,applet,japplet,Java,Swing,Applet,Japplet,这是我第一次在这里发帖。 我在做一些家庭作业时,第一次在java中使用GUI。我已经开始逐步编写一份意大利餐厅菜单 下面的代码编译良好,没有错误。编译后,我运行applet viewer意大利语.html,applet viewer屏幕只显示一个空白窗口。我有点困惑,因为我没有错误要处理。我错过了一些简单的东西 谢谢你的帮助 import javax.swing.*; import java.awt.*; public class Italian extends JApplet { //De
意大利语.html
,applet viewer屏幕只显示一个空白窗口。我有点困惑,因为我没有错误要处理。我错过了一些简单的东西
谢谢你的帮助
import javax.swing.*;
import java.awt.*;
public class Italian extends JApplet {
//Declare and array for a list of Pastas
private String [] pastas = {"Spaghetti", "Angel Hair Pasta", "Tortellini",
"Ziti"};
private String [] sauces = {"Maranaria", "Alfredo", "Spicy Marania"};
public Italian() {
//Create the base panel for the restaurant page
JPanel i1 = new JPanel();
i1.setLayout(new GridLayout(2, 1));
i1.add(new JComboBox(pastas));
i1.add(new JComboBox(sauces));
HTML
Java小程序演示
您尚未向小程序中添加任何内容以使小程序显示在屏幕上
无论是在构造函数中还是在init方法中,都需要将已创建的面板添加到内容窗格中
getContentPane().setLayout(new BorderLayout()); // Just to make sure
getContentPane().add(i1);
你应该在applet中添加一些东西,使它们工作 这将帮助您了解小程序和图形。 此示例将帮助您添加具有适当布局的不同按钮:
/*
This applet demonstrates various layout managers.
The applet itself uses a border layout with a JPanel in
the center, a JComboBox menu to the North, and a JLabel
to the south. The center panel uses a CardLayout.
Each card in the card layout contains a number of
buttons and uses a different layout manager. The
JComboBox menu is used to select among these cards.
The JLabel reports events as they occur.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutDemo extends JApplet
implements ActionListener, ItemListener {
CardLayout cards; // the layout manager for the center panel
JPanel cardPanel; // the center panel
JComboBox panelChoice; // menu for selecting which card to show
JLabel message; // a message shown at the bottom of the applet
public void init() {
panelChoice = new JComboBox(); // Set up the menu
panelChoice.setBackground(Color.white);
panelChoice.addItem("FlowLayout"); // Add in the names of the cards.
panelChoice.addItem("FlowLayout with Big Hgap");
panelChoice.addItem("Vertical BoxLayout");
panelChoice.addItem("Horizontal BoxLayout with Struts");
panelChoice.addItem("BorderLayout");
panelChoice.addItem("GridLayout(3,2)");
panelChoice.addItem("GridLayout(1,0)");
panelChoice.addItem("GridLayout(0,1)");
panelChoice.addItemListener(this);
message = new JLabel("Layout Demo", JLabel.CENTER); // Set up the mesage
message.setBackground(Color.white);
message.setOpaque(true); // so background color will show
message.setBorder(BorderFactory.createEmptyBorder(5,0,3,0));
message.setForeground(Color.red);
cardPanel = new JPanel(); // Set up the center panel
cardPanel.setBackground(Color.white);
cards = new CardLayout();
cardPanel.setLayout(cards);
setBackground(Color.blue);
getContentPane().setBackground(Color.blue);
getContentPane().setLayout(new BorderLayout(3,3));
getContentPane().add("Center",cardPanel);
getContentPane().add("North",panelChoice);
getContentPane().add("South",message);
JPanel panel; // Will represent various cards to be added to the center panel.
Box box; // For the cards that use a BoxLayout.
// Set up each "card" in the center panel to have its own layout
// manager and to contain a variety of buttons.
panel = new JPanel();
// use default FlowLayout for panel
panel.setBackground(Color.white);
cardPanel.add(panel, "FlowLayout");
addButton(panel,"First Button"); // ( addButton is a untility method, defined below )
addButton(panel,"Second Button");
addButton(panel,"Third Button");
addButton(panel,"Fourth Button");
addButton(panel,"Fifth Button");
addButton(panel,"Sixth Button");
addButton(panel,"Seventh Button");
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER,30000,5));
panel.setBackground(Color.white);
cardPanel.add(panel,"FlowLayout with Big Hgap");
addButton(panel," A Button");
addButton(panel,"Another Button");
addButton(panel,"A Third Button");
addButton(panel,"A Fourth Button");
addButton(panel,"A Final Button");
box = Box.createVerticalBox();
box.setBackground(Color.white);
cardPanel.add(box,"Vertical BoxLayout");
addButton(box,"Button One");
addButton(box,"Button Two");
addButton(box,"Button Three");
addButton(box,"Button Four");
addButton(box,"Button Five");
addButton(box,"Button Six");
box = Box.createHorizontalBox();
box.setBackground(Color.white);
cardPanel.add(box,"Horizontal BoxLayout with Struts");
addButton(box,"1st");
addButton(box,"2nd");
box.add( Box.createHorizontalStrut(10) );
addButton(box,"3rd");
addButton(box,"4th");
box.add( Box.createHorizontalStrut(10) );
addButton(box,"5th");
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
cardPanel.add(panel,"BorderLayout");
addButton(panel,"Center Button", BorderLayout.CENTER);
addButton(panel,"North Button", BorderLayout.NORTH);
addButton(panel,"South Button", BorderLayout.SOUTH);
addButton(panel,"East Button", BorderLayout.EAST);
addButton(panel,"West Button", BorderLayout.WEST);
panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(3,2)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
addButton(panel,"Button 5");
addButton(panel,"Button 6");
panel = new JPanel();
panel.setLayout(new GridLayout(1,0));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(1,0)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
panel = new JPanel();
panel.setLayout(new GridLayout(0,1));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(0,1)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
addButton(panel,"Button 5");
addButton(panel,"Button 6");
} // end init()
public Insets getInsets() {
// specify borders around the edges of the applet
return new Insets(3,3,3,3);
}
void addButton(Container p, String name) {
// Create a button with the given name and add it
// to the given panel. Set up the button to send
// events to the applet.
JButton b = new JButton(name);
p.add(b);
b.addActionListener(this);
}
void addButton(JPanel p, String name, Object option) {
// Same as above, but use the "option" object
// as an additional parameter in the add method.
JButton b = new JButton(name);
p.add(b, option);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
// A button was pressed. Report the name
// of the button by setting the message text.
String buttonName = evt.getActionCommand();
message.setText("Button \"" + buttonName + "\" was pressed.");
}
public void itemStateChanged(ItemEvent evt) {
// The user has selected an item from the JComboBox.
// Change the displayed card to match.
String panelName = (String)panelChoice.getSelectedItem();
cards.show(cardPanel, panelName);
message.setText("Panel \"" + panelName + "\" was selected.");
}
} // end class LayoutDemo
“第一次在java中使用GUI”,所以不要使用小程序。使用框架!小应用程序不是为新手准备的(如果你有一本书上面写着或暗示着其他内容,请从kindle上删除它,或者如果是纸上的,请用它来点燃)。为了更快地获得更好的帮助,请发布一篇文章。有趣的是,你说Andrew。我们的教授只想让我们使用applet。我希望我能用他作为引火物。。。。jk;)您在哪里将
i1
添加到italic
实例。顺便说一句-最好调用appletItalianMenu
,以避免与ItalianLanguage
或ItalianCar
或ItalianLover
之间的任何混淆,或者..我将其添加到这里JPanel i1=new JPanel();第12行。我刚刚写了一个简单的add,它在add(i1)中工作。我仍然有点困惑,将对此进行更多的调查。谢谢安德鲁,谢谢Mad@JaysonHartless:当你;如果您准备好了,您可以单击左侧的接受此答案。
/*
This applet demonstrates various layout managers.
The applet itself uses a border layout with a JPanel in
the center, a JComboBox menu to the North, and a JLabel
to the south. The center panel uses a CardLayout.
Each card in the card layout contains a number of
buttons and uses a different layout manager. The
JComboBox menu is used to select among these cards.
The JLabel reports events as they occur.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutDemo extends JApplet
implements ActionListener, ItemListener {
CardLayout cards; // the layout manager for the center panel
JPanel cardPanel; // the center panel
JComboBox panelChoice; // menu for selecting which card to show
JLabel message; // a message shown at the bottom of the applet
public void init() {
panelChoice = new JComboBox(); // Set up the menu
panelChoice.setBackground(Color.white);
panelChoice.addItem("FlowLayout"); // Add in the names of the cards.
panelChoice.addItem("FlowLayout with Big Hgap");
panelChoice.addItem("Vertical BoxLayout");
panelChoice.addItem("Horizontal BoxLayout with Struts");
panelChoice.addItem("BorderLayout");
panelChoice.addItem("GridLayout(3,2)");
panelChoice.addItem("GridLayout(1,0)");
panelChoice.addItem("GridLayout(0,1)");
panelChoice.addItemListener(this);
message = new JLabel("Layout Demo", JLabel.CENTER); // Set up the mesage
message.setBackground(Color.white);
message.setOpaque(true); // so background color will show
message.setBorder(BorderFactory.createEmptyBorder(5,0,3,0));
message.setForeground(Color.red);
cardPanel = new JPanel(); // Set up the center panel
cardPanel.setBackground(Color.white);
cards = new CardLayout();
cardPanel.setLayout(cards);
setBackground(Color.blue);
getContentPane().setBackground(Color.blue);
getContentPane().setLayout(new BorderLayout(3,3));
getContentPane().add("Center",cardPanel);
getContentPane().add("North",panelChoice);
getContentPane().add("South",message);
JPanel panel; // Will represent various cards to be added to the center panel.
Box box; // For the cards that use a BoxLayout.
// Set up each "card" in the center panel to have its own layout
// manager and to contain a variety of buttons.
panel = new JPanel();
// use default FlowLayout for panel
panel.setBackground(Color.white);
cardPanel.add(panel, "FlowLayout");
addButton(panel,"First Button"); // ( addButton is a untility method, defined below )
addButton(panel,"Second Button");
addButton(panel,"Third Button");
addButton(panel,"Fourth Button");
addButton(panel,"Fifth Button");
addButton(panel,"Sixth Button");
addButton(panel,"Seventh Button");
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER,30000,5));
panel.setBackground(Color.white);
cardPanel.add(panel,"FlowLayout with Big Hgap");
addButton(panel," A Button");
addButton(panel,"Another Button");
addButton(panel,"A Third Button");
addButton(panel,"A Fourth Button");
addButton(panel,"A Final Button");
box = Box.createVerticalBox();
box.setBackground(Color.white);
cardPanel.add(box,"Vertical BoxLayout");
addButton(box,"Button One");
addButton(box,"Button Two");
addButton(box,"Button Three");
addButton(box,"Button Four");
addButton(box,"Button Five");
addButton(box,"Button Six");
box = Box.createHorizontalBox();
box.setBackground(Color.white);
cardPanel.add(box,"Horizontal BoxLayout with Struts");
addButton(box,"1st");
addButton(box,"2nd");
box.add( Box.createHorizontalStrut(10) );
addButton(box,"3rd");
addButton(box,"4th");
box.add( Box.createHorizontalStrut(10) );
addButton(box,"5th");
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
cardPanel.add(panel,"BorderLayout");
addButton(panel,"Center Button", BorderLayout.CENTER);
addButton(panel,"North Button", BorderLayout.NORTH);
addButton(panel,"South Button", BorderLayout.SOUTH);
addButton(panel,"East Button", BorderLayout.EAST);
addButton(panel,"West Button", BorderLayout.WEST);
panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(3,2)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
addButton(panel,"Button 5");
addButton(panel,"Button 6");
panel = new JPanel();
panel.setLayout(new GridLayout(1,0));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(1,0)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
panel = new JPanel();
panel.setLayout(new GridLayout(0,1));
panel.setBackground(Color.white);
cardPanel.add(panel,"GridLayout(0,1)");
addButton(panel,"Button 1");
addButton(panel,"Button 2");
addButton(panel,"Button 3");
addButton(panel,"Button 4");
addButton(panel,"Button 5");
addButton(panel,"Button 6");
} // end init()
public Insets getInsets() {
// specify borders around the edges of the applet
return new Insets(3,3,3,3);
}
void addButton(Container p, String name) {
// Create a button with the given name and add it
// to the given panel. Set up the button to send
// events to the applet.
JButton b = new JButton(name);
p.add(b);
b.addActionListener(this);
}
void addButton(JPanel p, String name, Object option) {
// Same as above, but use the "option" object
// as an additional parameter in the add method.
JButton b = new JButton(name);
p.add(b, option);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
// A button was pressed. Report the name
// of the button by setting the message text.
String buttonName = evt.getActionCommand();
message.setText("Button \"" + buttonName + "\" was pressed.");
}
public void itemStateChanged(ItemEvent evt) {
// The user has selected an item from the JComboBox.
// Change the displayed card to match.
String panelName = (String)panelChoice.getSelectedItem();
cards.show(cardPanel, panelName);
message.setText("Panel \"" + panelName + "\" was selected.");
}
} // end class LayoutDemo