GUI Day 05

JPanel

package com.yudong.lesson05;

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
    public JPanelDemo(){
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10));

        JPanel panel = new JPanel(new GridLayout(1,3));

        panel.add(new JButton("1"));
        panel.add(new JButton("2"));
        panel.add(new JButton("3"));

        container.add(panel);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100,100,600,400);
    }

    public static void main(String[] args) {
        new JPanelDemo();
    }
}

JScroll

package com.yudong.lesson05;

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {

    public JScrollDemo() {
        Container container = this.getContentPane();
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("asdas士大夫士大夫士大夫士大夫士大夫胜多负少阿斯顿发射点学习ing");

        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setBounds(100,100,300,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

图片按钮、单选框、多选框

//将一个图片变成图标
URL resource = JButtonDemo.class.getResource("12.jpg");
Icon icon = new ImageIcon(resource);
//把图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);

//单选框
JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
JRadioButton radioButton03 = new JRadioButton("JRadioButton03");
//单选框只能选择一个,需进行分组
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
group.add(radioButton03);
container.add(radioButton01,BorderLayout.CENTER);
container.add(radioButton02,BorderLayout.NORTH);
container.add(radioButton03,BorderLayout.SOUTH);

//复选框
JCheckBox checkBox1 = new JCheckBox();
JCheckBox checkBox2 = new JCheckBox();
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox2,BorderLayout.SOUTH);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一种Java实现万年历并且带有GUI界面的方法。 首先,您需要使用Java Swing库来创建GUI界面。然后,您可以使用Java中的Calendar类来计算日期。 以下是一个简单的Java程序,可以实现万年历并带有GUI界面: ```java import java.awt.*; import java.awt.event.*; import java.util.Calendar; import javax.swing.*; public class CalendarGUI extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; JLabel label = new JLabel("", JLabel.CENTER); JButton b1 = new JButton("<<"), b2 = new JButton(">>"); JButton[] button = new JButton[49]; JPanel panel = new JPanel(new GridLayout(7, 7)); CalendarGUI() { super("万年历"); setSize(400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setResizable(false); panel.add(b1); for (int x = 0; x < button.length; x++) { final int selection = x; button[x] = new JButton(); button[x].setFocusPainted(false); button[x].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { label.setText(button[selection].getText()); } }); panel.add(button[x]); } panel.add(b2); add(panel, BorderLayout.CENTER); label.setFont(new Font("Serif", Font.BOLD, 24)); add(label, BorderLayout.NORTH); refreshCalendar(Calendar.getInstance()); } public void refreshCalendar(Calendar cal) { int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); label.setText(year + "年 " + (month + 1) + "月"); int startDay = cal.get(Calendar.DAY_OF_WEEK); int numberOfDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH); int weeks = cal.getActualMaximum(Calendar.WEEK_OF_MONTH); for (int x = 6; x < button.length; x++) { button[x].setText(""); } for (int x = 0, day = 1; day <= numberOfDays; x++) { for (int y = 0; y < 7; y++) { if (day == 1) { for (int i = 0; i < startDay - 1; i++) { button[x + i].setText(""); } } if (day <= numberOfDays) { button[x + startDay - 1].setText("" + day); day++; } } } } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == b1) { refreshCalendar(getPreviousMonth()); } else if (ae.getSource() == b2) { refreshCalendar(getNextMonth()); } } private Calendar getNextMonth() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, 1); return cal; } private Calendar getPreviousMonth() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); return cal; } public static void main(String[] args) { new CalendarGUI(); } } ``` 在此程序中,我们使用JFrame类创建了一个窗口,并使用GridLayout创建了一个7x7的网格布局来放置按钮。我们还使用了Calendar类来计算日期,以便在按钮上显示正确的日期。 在CalendarGUI类中,我们定义了一个refreshCalendar方法,该方法接受一个Calendar对象并更新按钮上的日期。我们还定义了两个方法,getPreviousMonth和getNextMonth,它们分别返回前一个月和下一个月的Calendar对象。 在main方法中,我们创建了一个CalendarGUI对象并显示了它。 希望这个简单的Java程序能够帮助您实现万年历并带有GUI界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值