Par*_*nak 2 java swing scrollbar jpanel jscrollpane
我有以下代码.执行下面的代码之后它显示了一个包含JInternalFrame的JFrame,它有一个JScrollPane这个JScrollPane有一个JPanel有很多输入控件.由于许多输入控件比JInternalFrame的大小更大,因此JPanel的大小因此我可滚动.
import java.awt.Dimension;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class InterfaceDesign {
public static void main(String args[]) {
InterfaceDesign id = new InterfaceDesign();
id.getPnlProjectDetail("My Project");
}
public void getPnlProjectDetail(String strProjectName) {
JPanel pnlPjDetail = new JPanel();
JScrollPane scrPjDetail;
pnlPjDetail.setLayout(null);
pnlPjDetail.setBounds(0, 0, 400, 400);
JFrame frmtest = new JFrame();
JInternalFrame interFrame = new JInternalFrame();
interFrame.setBounds(0, 0, 280, 180);
frmtest.setBounds(1, 1, 300, 200);
pnlPjDetail.setPreferredSize(new Dimension(400, 400));
JLabel lblFirstName = new JLabel("First Name");
JLabel lblLastName = new JLabel("Last Name");
JLabel lblAddress = new JLabel("Address");
JLabel lblCity = new JLabel("City");
JLabel lblZipCode = new JLabel("Zip Code");
JLabel lblPhone = new JLabel("Phone");
JLabel lblEmailID = new JLabel("Emain Id");
JTextField tfFirstName = new JTextField();
JTextField tfLastName = new JTextField();
JTextField tfAddress = new JTextField();
JTextField tfCity = new JTextField();
JTextField tfZipCode = new JTextField();
JTextField tfPhone = new JTextField();
JTextField tfEmailID = new JTextField();
lblFirstName.setBounds(25, 55, 85, 20);
tfFirstName.setBounds(25, 85, 85, 20);
pnlPjDetail.add(lblFirstName);
pnlPjDetail.add(tfFirstName);
lblLastName.setBounds(25, 115, 85, 20);
tfLastName.setBounds(25, 145, 85, 20);
pnlPjDetail.add(lblLastName);
pnlPjDetail.add(tfLastName);
lblAddress.setBounds(25, 175, 85, 20);
tfAddress.setBounds(25, 205, 85, 20);
pnlPjDetail.add(lblAddress);
pnlPjDetail.add(tfAddress);
lblCity.setBounds(25, 235, 85, 20);
tfCity.setBounds(25, 265, 85, 20);
pnlPjDetail.add(lblCity);
pnlPjDetail.add(tfCity);
lblZipCode.setBounds(25, 295, 85, 20);
tfZipCode.setBounds(25, 325, 85, 20);
pnlPjDetail.add(lblZipCode);
pnlPjDetail.add(tfZipCode);
lblPhone.setBounds(25, 355, 85, 20);
tfPhone.setBounds(25, 385, 85, 20);
pnlPjDetail.add(lblPhone);
pnlPjDetail.add(tfPhone);
lblEmailID.setBounds(25, 415, 85, 20);
tfEmailID.setBounds(25, 445, 85, 20);
pnlPjDetail.add(lblEmailID);
pnlPjDetail.add(tfEmailID);
scrPjDetail = new JScrollPane(pnlPjDetail);
scrPjDetail.setAutoscrolls(true);
//frmtest.setContentPane(scrProjectDetail);
interFrame.setContentPane(scrPjDetail);
JDesktopPane dpane = new JDesktopPane();
interFrame.setVisible(true);
dpane.add(interFrame);
//frmtest.getLayeredPane().add(interFrame);
frmtest.setContentPane(dpane);
//frmtest.add(scrProjectDetail);
frmtest.setVisible(true);
frmtest.setResizable(false);
frmtest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//return pnlPjDetail;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要你的指导来解决以下问题/疑虑
<JFrame>.getContentPane.add(<JInternalFrame>);
简单的任务,令人惊讶的古怪解决方案 - 就个人而言,我判断每个必须依赖焦点的应用程序代码解决方案古怪;-)
无法提供任何不依赖于焦点的东西:只需要在窗体上的每个组件上不需要focusListener,就可以稍微维护一下.相反,使用KeyboardFocusManager注册PropertyChangeListener,并在收到有关其permanentFocusOwner属性更改的通知时进行滚动
public static class FocusDrivenScroller implements PropertyChangeListener {
private JComponent parent;
public FocusDrivenScroller(JComponent parent) {
this.parent = parent;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
Component focused = (Component) evt.getNewValue();
if (focused == null || !SwingUtilities.isDescendingFrom(focused, parent)) return;
parent.scrollRectToVisible(focused.getBounds());
}
}
Run Code Online (Sandbox Code Playgroud)
要在应用程序代码中使用,请使用驻留在JScrollPane中的表单进行实例化
public void buildAndShowDetailsFrame(String strProjectName) {
// the container to scroll such that a focused child
// is visible when gaining focus
JPanel detailsForm = new JPanel();
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener("permanentFocusOwner",
new FocusDrivenScroller(detailsForm));
// choose and use an appropriate LayoutManager
// note: this is only an example!
// real-world requirements most probably need a stronger one
detailsForm.setLayout(new BoxLayout(detailsForm, BoxLayout.PAGE_AXIS));
// quick fill with stuff
String[] labels = {"First Name", "Last Name",
"Address", "City", "Zip Code", "Phone", "Emain Id"};
for (String string : labels) {
detailsForm.add(new JLabel(string));
detailsForm.add(new JTextField());
}
JFrame frame = new JFrame();
frame.add(new JScrollPane(detailsForm));
frame.pack();
// force scrollbar to appear
frame.setSize(frame.getWidth(), frame.getHeight()/2);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Run Code Online (Sandbox Code Playgroud)
除了这些方法的第一行,它只是OP代码的缩短版本(引诱他/她使用适当的LayoutManager :-)
归档时间: |
|
查看次数: |
2693 次 |
最近记录: |