我对编码很新,我只是想创建一个简单的GUI。我的代码的目标是设计一个看起来像这样的应用程序: !http://curriculum.kcdistancelearning.com/courses/PROG2s-HS-A08/s/unit7/resources/images/JV_7.5.11.JPG![代码目标示例] 我一直在使用窗口构建器,我喜欢它在构建器内部的样子。我的问题是当我尝试将我的代码作为Java应用程序运行时。显示“选择不包含applet”的错误我想我可能错过了一些东西。我不确定我错过了什么,或者它会是什么。
以下是我的代码目前的样子:
`package Create.GUI`
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SpringLayout;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import java.awt.TextArea;
import java.awt.Button;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Canvas;
import javax.swing.JProgressBar;
import javax.swing.JToggleButton;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollBar;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import java.awt.BorderLayout;
public class StringPannel1 extends JFrame
{
private StringPannel1 currentPannel;
public StringPannel1() {
getContentPane().setBackground(Color.WHITE);
SpringLayout springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
JButton btnNewButton = new JButton("Button");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 23, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 23, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton, 88, SpringLayout.NORTH, getContentPane());
getContentPane().add(btnNewButton);
txtTextFeild = new JTextField();
txtTextFeild.setBackground(new Color(248, 248, 255));
springLayout.putConstraint(SpringLayout.NORTH, txtTextFeild, 0, SpringLayout.NORTH, btnNewButton);
springLayout.putConstraint(SpringLayout.WEST, txtTextFeild, 27, SpringLayout.EAST, btnNewButton);
springLayout.putConstraint(SpringLayout.SOUTH, txtTextFeild, 0, SpringLayout.SOUTH, btnNewButton);
springLayout.putConstraint(SpringLayout.EAST, txtTextFeild, -53, SpringLayout.EAST, getContentPane());
txtTextFeild.setText("Text Feild");
getContentPane().add(txtTextFeild);
txtTextFeild.setColumns(10);
JScrollBar scrollBar = new JScrollBar();
springLayout.putConstraint(SpringLayout.NORTH, scrollBar, 17, SpringLayout.SOUTH, btnNewButton);
springLayout.putConstraint(SpringLayout.WEST, scrollBar, 23, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, scrollBar, 130, SpringLayout.SOUTH, btnNewButton);
getContentPane().add(scrollBar);
JTextArea txtrTextArea = new JTextArea();
txtrTextArea.setBackground(new Color(248, 248, 255));
springLayout.putConstraint(SpringLayout.NORTH, txtrTextArea, 0, SpringLayout.NORTH, scrollBar);
springLayout.putConstraint(SpringLayout.WEST, txtrTextArea, 7, SpringLayout.EAST, scrollBar);
springLayout.putConstraint(SpringLayout.SOUTH, txtrTextArea, 0, SpringLayout.SOUTH, scrollBar);
springLayout.putConstraint(SpringLayout.EAST, txtrTextArea, 173, SpringLayout.EAST, scrollBar);
txtrTextArea.setText("Text Area");
getContentPane().add(txtrTextArea);
JCheckBox chckbxCheckBox = new JCheckBox("Check Box");
chckbxCheckBox.setBackground(new Color(255, 255, 255));
springLayout.putConstraint(SpringLayout.NORTH, chckbxCheckBox, 58, SpringLayout.SOUTH, txtTextFeild);
springLayout.putConstraint(SpringLayout.WEST, chckbxCheckBox, 6, SpringLayout.EAST, txtrTextArea);
getContentPane().add(chckbxCheckBox);
JLabel lblLabel = new JLabel("Label");
springLayout.putConstraint(SpringLayout.NORTH, lblLabel, 15, SpringLayout.SOUTH, txtrTextArea);
springLayout.putConstraint(SpringLayout.WEST, lblLabel, 104, SpringLayout.WEST, getContentPane());
lblLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
getContentPane().add(lblLabel);
}
private JTextField txtTextFelid;
/**
* @wbp.nonvisual location=-193,109
*/
private final JButton button = new JButton("New button");
private JTextField txtTextFeild;
public void StringPannel()
{
currentPannel = new StringPannel1();
setupFrame();
}
private void setupFrame()
{
this.setVisible(true);
this.setContentPane(currentPannel);
this.setSize(350, 350);
this.setDefultCloseOpperation(JFrame.EXIT_ON_CLOSE);
}
private void setDefultCloseOpperation(int exitOnClose) {
// TODO Auto-generated method stub
}
{
{
setupPannel1()
}
private void setupPannel()
{
setBorder(new LineBorder(new Color(0, 0, 255)));
setBackground(new Color(255, 255, 255));
SpringLayout springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
}
private void setBorder(LineBorder lineBorder) {
// TODO Auto-generated method stub
}
}
感谢所有帮助!
答案 0 :(得分:1)
基本上,您的应用程序来自JFrame
而不是JApplet
。
两者之间存在(重大)差异......
我会是第一个说出来的人......
JPanel
创建核心用户界面,然后将此/这些添加到您要使用的容器(JFrame
或JApplet
),它会使您的代码更具可重用性,允许您按照自己的意愿部署应用程序,而不必诉诸某种黑客...... 如果您一心想开发applet,请查看Lesson: Java Applets了解更多详情