Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Tại sao Frame không hiển thị các thành phần nhỉ?

    Mình mới học sang phần GUI, mọi người xem giùm mình đoạn code này, tại sao khi chạy thì ở size đã set thì không có thành phần nào, phải click phóng to cửa sổ ra thì các thành phần mới hiện lên?



    Mã:
    /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package baitap; import java.awt.Color;import java.awt.Cursor;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.*; /** * * @author Nam™ */class init extends JFrame {    JButton button=null;    JPanel panel=null;    JComboBox combobox=null;     public init() {        this.setSize(500, 200);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setResizable(false);        this.setVisible(true);        this.setLocation(500, 300);         this.setTitle("Bai tap 1");                panel = new JPanel();        panel.setBackground(Color.white);        panel.setLayout(new FlowLayout(FlowLayout.CENTER));                String[] color = {"Blue", "Green", "Red"};        combobox = new JComboBox(color);        combobox.setSelectedIndex(-1);        combobox.setPreferredSize(new Dimension(200, 25));        combobox.setMaximumSize(new Dimension(210, 25));        combobox.addItemListener(new ItemListen());        panel.add(combobox);        this.add(panel);             }    public class ItemListen implements ItemListener{         @Override        public void itemStateChanged(ItemEvent ie) {                if(ie.getStateChange() == ItemEvent.SELECTED){                    String e = combobox.getSelectedItem().toString();                    if(e.equals("Green")){                        panel.setBackground(Color.green);                    }                    if(e.equals("Red")){                        panel.setBackground(Color.red);                    }                    if(e.equals("Blue")){                        panel.setBackground(Color.BLUE);                    }                                    }        }                     }    } public class bt1 {    public static void main(String[] args) {        new init();    }}

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •