我有以下代码。执行下面的代码之后它显示了一个包含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;
}
}
我需要你的指导来解决以下问题/疑惑
<JFrame>.getContentPane.add(<JInternalFrame>);
答案 0 :(得分:6)
简单的任务,令人惊讶的古怪解决方案 - 就个人而言,我判断每个必须依赖焦点的应用程序代码解决方案古怪;-)
无法提供任何不依赖于焦点的内容:只需要在窗体上的每个组件上不需要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());
}
}
要在应用程序代码中使用,请使用驻留在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);
}
除了这些方法的第一行之外,它只是OP代码的缩短版本(引诱他/她使用适当的LayoutManager :-)
答案 1 :(得分:3)
Scrolling a Form是对Kleopartra建议的一种奇特的实现。
答案 2 :(得分:0)
关于滚动问题:您可以例如添加一个侦听器来聚焦事件并相应地滚动窗格。
FocusAdapter scrollFocusListener = new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
System.out.println(((JComponent) e.getSource()).getBounds());
scrPjDetail.getViewport().scrollRectToVisible(((JComponent) e.getSource()).getBounds());
}
};
tfFirstName.addFocusListener(scrollFocusListener);
tfLastName.addFocusListener(scrollFocusListener);
tfAddress.addFocusListener(scrollFocusListener);
...
答案 3 :(得分:0)
试试这个:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JComponent;
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.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class InterfaceDesign {
private SrcollListener l = new SrcollListener();
public static void main( String args[] ) {
InterfaceDesign id = new InterfaceDesign();
id.getPnlProjectDetail( "My Project" );
}
public void getPnlProjectDetail( String strProjectName ) {
JPanel pnlPjDetail = new JPanel();
JPanel pnlPjDetail2 = new JPanel();
JScrollPane scrPjDetail;
JScrollPane scrPjDetail2;
pnlPjDetail.setLayout( null );
pnlPjDetail.setBounds( 0, 0, 200, 250 );
pnlPjDetail2.setLayout( null );
pnlPjDetail2.setBounds( 0, 0, 200, 300 );
JFrame frmtest = new JFrame();
JInternalFrame interFrame = new JInternalFrame();
interFrame.setBounds( 0, 0, 280, 180 );
frmtest.setBounds( 1, 1, 600, 200 );
pnlPjDetail.setPreferredSize( new Dimension( 200, 250 ) );
pnlPjDetail2.setPreferredSize( new Dimension( 200, 300 ) );
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 );
tfFirstName.addFocusListener( l );
pnlPjDetail.add( lblFirstName );
pnlPjDetail.add( tfFirstName );
lblLastName.setBounds( 25, 115, 85, 20 );
tfLastName.setBounds( 25, 145, 85, 20 );
tfLastName.addFocusListener( l );
pnlPjDetail.add( lblLastName );
pnlPjDetail.add( tfLastName );
lblAddress.setBounds( 25, 175, 85, 20 );
tfAddress.setBounds( 25, 205, 85, 20 );
tfAddress.addFocusListener( l );
pnlPjDetail.add( lblAddress );
pnlPjDetail.add( tfAddress );
lblCity.setBounds( 25, 55, 85, 20 );
tfCity.setBounds( 25, 85, 85, 20 );
tfCity.addFocusListener( l );
pnlPjDetail2.add( lblCity );
pnlPjDetail2.add( tfCity );
lblZipCode.setBounds( 25, 115, 85, 20 );
tfZipCode.setBounds( 25, 145, 85, 20 );
tfZipCode.addFocusListener( l );
pnlPjDetail2.add( lblZipCode );
pnlPjDetail2.add( tfZipCode );
lblPhone.setBounds( 25, 175, 85, 20 );
tfPhone.setBounds( 25, 205, 85, 20 );
tfPhone.addFocusListener( l );
pnlPjDetail2.add( lblPhone );
pnlPjDetail2.add( tfPhone );
lblEmailID.setBounds( 25, 235, 85, 20 );
tfEmailID.setBounds( 25, 265, 85, 20 );
tfEmailID.addFocusListener( l );
pnlPjDetail2.add( lblEmailID );
pnlPjDetail2.add( tfEmailID );
scrPjDetail = new JScrollPane( pnlPjDetail );
scrPjDetail2 = new JScrollPane( pnlPjDetail2 );
scrPjDetail.setAutoscrolls( true );
//frmtest.setContentPane(scrProjectDetail);
JSplitPane splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, scrPjDetail, scrPjDetail2 );
splitPane.setDividerLocation( 300 );
interFrame.setContentPane( splitPane );
interFrame.setVisible( true );
//frmtest.getLayeredPane().add(interFrame);
frmtest.setContentPane( interFrame );
//frmtest.add(scrProjectDetail);
frmtest.setVisible( true );
frmtest.setResizable( false );
frmtest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//return pnlPjDetail;
}
public class SrcollListener implements FocusListener {
@Override
public void focusGained( FocusEvent e ) {
final Component component = e.getComponent();
if( component != null && component.getParent() != null && component.getParent() instanceof JComponent ) {
SwingUtilities.invokeLater( new Runnable() {
@Override
public void run() {
((JComponent)component.getParent()).scrollRectToVisible( component.getBounds() );
}
} );
}
}
@Override
public void focusLost( FocusEvent e ) {
}
}
}