要求用java设计一个窗口,我设计完成后,效果如下,我该怎么改 。哪位好心人帮帮忙,谢谢
我自己写的代码:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing...
我自己写的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class sc extends JFrame implements ActionListener{
JTextArea tf=new JTextArea();
JMenuBar bar=new JMenuBar();
JMenu menu1=new JMenu("File");
JMenu menu2=new JMenu("View");
JMenu menu3=new JMenu("Help");
JMenuItem w1=new JMenuItem("New");
JMenuItem w2=new JMenuItem("Open");
JMenuItem w3=new JMenuItem("Save");
JMenuItem w4=new JMenuItem("Exit");
JMenuItem w5=new JMenuItem("1");
JMenuItem w7=new JMenuItem("3");
JMenuItem w8=new JMenuItem("4");
JMenuItem w9=new JMenuItem("5");
JMenuItem w10=new JMenuItem("6");
public sc()
{
super("SocksCap Control");
getContentPane().add(new JScrollPane(tf));
tf.setEditable(false);
bar.setOpaque(true);
setJMenuBar(bar);
menu1.add(w1);
menu1.add(w2);
menu1.add(w3);
menu1.add(w4);
menu2.add(w5);
menu2.add(w6);
menu2.add(w7);
menu3.add(w8);
menu3.add(w9);
menu3.add(w10);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
w1.addActionListener(this);
w2.addActionListener(this);
w3.addActionListener(this);
w4.addActionListener(this);
addWindowListener(new win());
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==w1)tf.setText("新建");
if(e.getSource()==w2)tf.setText("打开");
if(e.getSource()==w3)tf.setText("保存");
if(e.getSource()==w4)tf.setText("退出");
}
class win extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ System.exit(0); }
}
public static void main(String[] args)
{
JFrame f=new sc();
f.setSize(400, 200);
f.setVisible(true);
sc Ta=new sc();
Ta.go();
}
public void go(){
JFrame frame=new JFrame();
JButton b1=new JButton("New",new ImageIcon("new.jpg"));
JButton b2=new JButton("Delete",new ImageIcon("delete.jpg"));
JButton b3=new JButton("Modify",new ImageIcon("modify.jpg"));
JButton b4=new JButton("Run",new ImageIcon("run.jpg"));
JPanel pane1=new JPanel();
JPanel pane2=new JPanel();
pane1.add(b1);
pane1.add(b2);
pane1.add(b3);
pane1.add(b4);
Container cp=frame.getContentPane();
cp.setLayout(new GridLayout(0,1));
cp.add(pane1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
运行完成后 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class sc extends JFrame implements ActionListener{
JTextArea tf=new JTextArea();
JMenuBar bar=new JMenuBar();
JMenu menu1=new JMenu("File");
JMenu menu2=new JMenu("View");
JMenu menu3=new JMenu("Help");
JMenuItem w1=new JMenuItem("New");
JMenuItem w2=new JMenuItem("Open");
JMenuItem w3=new JMenuItem("Save");
JMenuItem w4=new JMenuItem("Exit");
JMenuItem w5=new JMenuItem("1");
JMenuItem w7=new JMenuItem("3");
JMenuItem w8=new JMenuItem("4");
JMenuItem w9=new JMenuItem("5");
JMenuItem w10=new JMenuItem("6");
public sc()
{
super("SocksCap Control");
getContentPane().add(new JScrollPane(tf));
tf.setEditable(false);
bar.setOpaque(true);
setJMenuBar(bar);
menu1.add(w1);
menu1.add(w2);
menu1.add(w3);
menu1.add(w4);
menu2.add(w5);
menu2.add(w6);
menu2.add(w7);
menu3.add(w8);
menu3.add(w9);
menu3.add(w10);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
w1.addActionListener(this);
w2.addActionListener(this);
w3.addActionListener(this);
w4.addActionListener(this);
addWindowListener(new win());
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==w1)tf.setText("新建");
if(e.getSource()==w2)tf.setText("打开");
if(e.getSource()==w3)tf.setText("保存");
if(e.getSource()==w4)tf.setText("退出");
}
class win extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ System.exit(0); }
}
public static void main(String[] args)
{
JFrame f=new sc();
f.setSize(400, 200);
f.setVisible(true);
sc Ta=new sc();
Ta.go();
}
public void go(){
JFrame frame=new JFrame();
JButton b1=new JButton("New",new ImageIcon("new.jpg"));
JButton b2=new JButton("Delete",new ImageIcon("delete.jpg"));
JButton b3=new JButton("Modify",new ImageIcon("modify.jpg"));
JButton b4=new JButton("Run",new ImageIcon("run.jpg"));
JPanel pane1=new JPanel();
JPanel pane2=new JPanel();
pane1.add(b1);
pane1.add(b2);
pane1.add(b3);
pane1.add(b4);
Container cp=frame.getContentPane();
cp.setLayout(new GridLayout(0,1));
cp.add(pane1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
运行完成后 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询