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(); }}
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...