java中scrollpane的意思_java-使用ScrollPane自动滚动JTextArea?

永远不要这样做:

actionLog.setPreferredSize(new Dimension(500, 300));

由于这样做会人为地限制JTextArea的大小,从而导致当前使您烦恼的效果.另请注意,通常最好避免在任何内容上设置首选大小.

而是设置JTextARea的列数和行数.这可以通过setter方法或通过简单的构造函数调用来完成:JTextArea myTextArea = new JTextArea(rows,columns);

顺便说一句:我想知道JList是否对您更好.

import javax.swing.*;

import javax.swing.text.DefaultCaret;

public class Main2 {

private static void createAndShowGUI() {

JPanel mainPanel = new ControlPanel();

JFrame frame = new JFrame("Main2");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(mainPanel);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

class ControlPanel extends JPanel {

private static final int LOG_ROWS = 15;

private static final int LOG_COLS = 40;

private JButton resetButton = new JButton("Reset");

private JPanel logPanel = new JPanel();

private JLabel actionLogsLabel = new JLabel("Action Log");

private JLabel pointsLogsLabel = new JLabel("Points Log");

private JTextArea actionLog = new JTextArea();

private JTextArea pointsLog = new JTextArea();

private JScrollPane actionScroll;

private JScrollPane pointsScroll;

public ControlPanel() {

init();

this.add(resetButton);

this.add(logPanel);

}

private void init() {

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

this.setAlignmentX(LEFT_ALIGNMENT);

this.logPanel.setLayout(new BoxLayout(logPanel, BoxLayout.Y_AXIS));

this.logPanel.setAlignmentX(LEFT_ALIGNMENT);

// !! actionLog.setPreferredSize(new Dimension(500, 300));

// !! actionLog.setMaximumSize(actionLog.getPreferredSize());

actionLog.setRows(LOG_ROWS); // !!

actionLog.setColumns(LOG_COLS); // !!

actionLog.setEditable(false);

actionLog.setWrapStyleWord(true);

DefaultCaret caret = (DefaultCaret) actionLog.getCaret();

caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

// !! pointsLog.setPreferredSize(new Dimension(500, 300));

// !! pointsLog.setMaximumSize(pointsLog.getPreferredSize());

pointsLog.setRows(LOG_ROWS); // !!

pointsLog.setColumns(LOG_COLS); // !!

pointsLog.setEditable(false);

pointsLog.setWrapStyleWord(true);

pointsScroll = new JScrollPane(pointsLog,

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

actionScroll = new JScrollPane(actionLog,

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

logPanel.add(actionLogsLabel);

logPanel.add(actionScroll);

for (int i = 0; i < 500; i++) {

actionLog.setText(actionLog.getText() + "Line: " + i + "

");

}

logPanel.add(pointsLogsLabel);

logPanel.add(pointsScroll);

}

}

编辑

嵌套布局和JLists的示例:

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.*;

public class Main2B {

private static void createAndShowGUI() {

ControlPanel2B controlPanel = new ControlPanel2B();

controlPanel.setBorder(BorderFactory.createEtchedBorder());

JPanel mainPanel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

mainPanel.add(controlPanel, gbc);

JFrame frame = new JFrame("Main2");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(mainPanel);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

@SuppressWarnings("serial")

class ControlPanel2B extends JPanel {

private static final int LOG_ROWS = 15;

private static final int LIST_WIDTH = 500;

private JButton resetButton = new JButton("Reset");

private JPanel logPanel = new JPanel();

private JLabel actionLogsLabel = new JLabel("Action Log");

private JLabel pointsLogsLabel = new JLabel("Points Log");

private DefaultListModel actionLogListModel = new DefaultListModel<>();

private JList actionLogList = new JList(actionLogListModel);

private DefaultListModel pointsLogListModel = new DefaultListModel<>();

private JList pointsLogList = new JList(pointsLogListModel);

private JScrollPane actionScroll;

private JScrollPane pointsScroll;

public ControlPanel2B() {

init();

this.add(resetButton);

this.add(logPanel);

}

private void init() {

actionLogList.setVisibleRowCount(LOG_ROWS);

pointsLogList.setVisibleRowCount(LOG_ROWS);

actionLogList.setFixedCellWidth(LIST_WIDTH);

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

this.setAlignmentX(LEFT_ALIGNMENT);

this.logPanel.setLayout(new BoxLayout(logPanel, BoxLayout.Y_AXIS));

this.logPanel.setAlignmentX(LEFT_ALIGNMENT);

pointsScroll = new JScrollPane(pointsLogList,

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

actionScroll = new JScrollPane(actionLogList,

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

logPanel.add(actionLogsLabel);

logPanel.add(actionScroll);

for (int i = 0; i < 500; i++) {

actionLogListModel.addElement("Line: " + i);

}

logPanel.add(pointsLogsLabel);

logPanel.add(pointsScroll);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解你的问题。你想知道如何使用Java构建一个简单的界面通讯录。 首先,你需要选择一个Java GUI库,比如Swing或JavaFX。在这个例子,我将使用Swing。 接下来,你需要创建一个通讯录类,该类将存储联系人的信息。你可以使用Java集合框架的HashMap来存储联系人信息,其键是联系人的名称,值是联系人的详细信息。 下面是一个简单的通讯录类的示例代码: ``` import java.util.HashMap; public class AddressBook { private HashMap<String, String> contacts; public AddressBook() { contacts = new HashMap<String, String>(); } public void addContact(String name, String info) { contacts.put(name, info); } public String getContactInfo(String name) { return contacts.get(name); } public void removeContact(String name) { contacts.remove(name); } } ``` 现在,你需要创建一个图形用户界面来与通讯录类交互。你可以使用Swing库的JFrame、JPanel、JButton等组件来创建界面。 下面是一个简单的通讯录GUI界面的示例代码: ``` import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class AddressBookGUI extends JFrame { private AddressBook addressBook; private JTextArea displayArea; private JTextField nameField, infoField; public AddressBookGUI() { super("Address Book"); addressBook = new AddressBook(); JPanel inputPanel = new JPanel(new GridLayout(3, 2)); inputPanel.add(new JLabel("Name:")); nameField = new JTextField(); inputPanel.add(nameField); inputPanel.add(new JLabel("Info:")); infoField = new JTextField(); inputPanel.add(infoField); JButton addButton = new JButton("Add"); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = nameField.getText(); String info = infoField.getText(); addressBook.addContact(name, info); displayArea.append("Added contact: " + name + "\n"); nameField.setText(""); infoField.setText(""); } }); JButton removeButton = new JButton("Remove"); removeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = nameField.getText(); addressBook.removeContact(name); displayArea.append("Removed contact: " + name + "\n"); nameField.setText(""); infoField.setText(""); } }); JPanel buttonPanel = new JPanel(new GridLayout(1, 2)); buttonPanel.add(addButton); buttonPanel.add(removeButton); displayArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(displayArea); getContentPane().add(inputPanel, BorderLayout.NORTH); getContentPane().add(buttonPanel, BorderLayout.CENTER); getContentPane().add(scrollPane, BorderLayout.SOUTH); setSize(400, 300); setVisible(true); } public static void main(String[] args) { AddressBookGUI gui = new AddressBookGUI(); } } ``` 在这个GUI,你可以输入联系人的名称和详细信息,并使用“添加”按钮将它们添加到通讯录。你还可以使用“删除”按钮删除现有联系人。 当你点击“添加”或“删除”按钮时,GUI将调用通讯录类的方法来更新通讯录。在更新通讯录后,GUI会将操作结果显示在文本区域。 这只是一个简单的通讯录GUI的例子,你可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值