sinat_34319069
sinat_34319069
2016-03-24 14:29
采纳率: 100%
浏览 4.6k
已采纳

Java swing 怎么将文本域添加到面板上?

Java swing 怎么将文本域添加到面板上?初学,求大神指教,有代码说明更好,谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • luochoudan
    丑旦 2016-03-24 14:40
    已采纳
    点赞 评论
  • ScanQ
    王新港 2016-03-24 14:50

    从书上拍的 不知道楼主问的是不是这个图片图片

    点赞 评论
  • j314_hehe
    OSError 2016-03-24 14:58

    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.UIManager;
    import javax.swing.JLabel;
    import java.awt.Font;

    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import javax.swing.SwingConstants;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.net.URL;

    public class MessageDialogIcon extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MessageDialogIcon frame = new MessageDialogIcon();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public MessageDialogIcon() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 338, 171);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
    
        JLabel label = new JLabel("\u4E2A\u4EBA\u94F6\u884C\u63D0\u6B3E\u673A");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setFont(new Font("SansSerif", Font.PLAIN, 24));
        label.setBounds(6, 6, 310, 37);
        contentPane.add(label);
    
        JLabel label_1 = new JLabel("\u63D0\u6B3E\u91D1\u989D\uFF1A");
        label_1.setBounds(6, 55, 71, 18);
        contentPane.add(label_1);
    
        textField = new JTextField();
        textField.setBounds(67, 49, 236, 30);
        contentPane.add(textField);
        textField.setColumns(10);
    
        JButton button = new JButton("\u63D0\u6B3E");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_button_actionPerformed(arg0);
            }
        });
        button.setBounds(124, 91, 90, 30);
        contentPane.add(button);
    }
    
    protected void do_button_actionPerformed(ActionEvent arg0) {
        String text = textField.getText();// 获取文本框输入
        URL resource = getClass().getResource("money.png");// 获取资源文件路径
        ImageIcon icon = new ImageIcon(resource);// 创建图标对象
        // 显示带有自定义图标的信息提示对话框
        JOptionPane.showMessageDialog(this, "你在我这存“" + text + "”这些钱了吗", "取钱啊?",
                JOptionPane.QUESTION_MESSAGE, icon);
    }
    

    }

    点赞 评论

相关推荐