java GUI JPanel面板添加滚动条

8 篇文章 1 订阅

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class JscrollPaneText extends JFrame {
public static void main(String[] args) {
new JscrollPaneText();
}
private JButton[] jbs={
new JButton(“向下拉滚条”),
new JButton(“向上拉滚动条”)
};

private JPanel jp=new JPanel();

private JScrollPane jspg=new JScrollPane(jp,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

public JscrollPaneText()  {
	super();
	
	this.add(jspg);
	
	this.jp.setLayout(null);

	/*
	 * 设置panel的首选大小,同时保证宽高大于JScrollPane的宽高,这样下面的
	 * 	JScrollPane才会出现滚动条
	 */	
	this.jp.setPreferredSize(new Dimension(800,2000));//去了这一条滚动条没有反应
	for (int i = 0; i < jbs.length; i++) {
	jp.add(jbs[i]);
		jbs[i].setBounds(50, 50+1500*i, 150, 40);
	}
	
	this.setTitle("为面板加滚动条");
	this.setBounds(200, 200, 400, 500);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setVisible(true);
}

}
借鉴:https://www.cnblogs.com/fdzang/p/9601010.html

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的Java GUI宠物饲养系统的示例代码,仅供参考: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PetSystem extends JFrame implements ActionListener { private JTextField nameField; private JTextField typeField; private JTextField ageField; private JTextField weightField; private JButton addButton; private JTextArea petListArea; public PetSystem() { super("宠物饲养系统"); setSize(400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建添加宠物的组件 JLabel nameLabel = new JLabel("宠物名称:"); nameField = new JTextField(20); JLabel typeLabel = new JLabel("宠物种类:"); typeField = new JTextField(20); JLabel ageLabel = new JLabel("宠物年龄:"); ageField = new JTextField(20); JLabel weightLabel = new JLabel("宠物体重:"); weightField = new JTextField(20); addButton = new JButton("添加"); addButton.addActionListener(this); // 将添加宠物的组件添加面板JPanel addPanel = new JPanel(new GridLayout(5, 2)); addPanel.add(nameLabel); addPanel.add(nameField); addPanel.add(typeLabel); addPanel.add(typeField); addPanel.add(ageLabel); addPanel.add(ageField); addPanel.add(weightLabel); addPanel.add(weightField); addPanel.add(addButton); // 创建宠物列表的组件 JLabel petListLabel = new JLabel("宠物列表:"); petListArea = new JTextArea(15, 30); petListArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(petListArea); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // 将宠物列表的组件添加面板JPanel petListPanel = new JPanel(new BorderLayout()); petListPanel.add(petListLabel, BorderLayout.NORTH); petListPanel.add(scrollPane, BorderLayout.CENTER); // 将面板添加到窗口中 JPanel panel = new JPanel(new BorderLayout()); panel.add(addPanel, BorderLayout.NORTH); panel.add(petListPanel, BorderLayout.CENTER); add(panel); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // 获取用户输入的宠物信息 String name = nameField.getText(); String type = typeField.getText(); int age = Integer.parseInt(ageField.getText()); double weight = Double.parseDouble(weightField.getText()); // 创建宠物对象并添加到列表中 Pet pet = new Pet(name, type, age, weight); petListArea.append(pet.toString() + "\n"); // 清空用户输入的宠物信息 nameField.setText(""); typeField.setText(""); ageField.setText(""); weightField.setText(""); } public static void main(String[] args) { new PetSystem(); } } class Pet { private String name; private String type; private int age; private double weight; public Pet(String name, String type, int age, double weight) { this.name = name; this.type = type; this.age = age; this.weight = weight; } public String getName() { return name; } public String getType() { return type; } public int getAge() { return age; } public double getWeight() { return weight; } @Override public String toString() { return "名称:" + name + ",种类:" + type + ",年龄:" + age + "岁,体重:" + weight + "kg"; } } ``` 在这个例子中,用户可以在添加宠物的面板中输入宠物的名称、种类、年龄和体重,并点击“添加”按钮将宠物添加到宠物列表中。宠物列表使用JTextArea组件实现,并使用JScrollPane组件添加滚动条。当用户点击“添加”按钮后,程序会创建一个Pet对象并将它添加到宠物列表中,并清空用户输入的宠物信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值