我可以使用添加到JToolBar的JButtons滚动JPanel吗?当我生成大量缩略图时,它们并不都适合JPanel。我想使用上/下箭头按钮来滚动。可以这样做吗?如果可以,怎么做?
注意:我试图在没有JScrollPane的情况下完成这项工作,因为我想要的是自定义箭头图标,而不是标准的滚动条。
以下是SSCCE:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class PicSlider2 {
private JButton thumbs;
private JButton[] thumbnails;
private JLabel picViewer;
private JPanel thumbPanel;
private JToolBar toolBar;
public PicSlider2() {
final JFrame frame = new JFrame("Picture Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 600));
frame.setResizable(false);
frame.setLayout(new BorderLayout());
picViewer = new JLabel();
picViewer.setText("Image here");
picViewer.setHorizontalAlignment(JLabel.CENTER);
picViewer.setVerticalAlignment(JLabel.BOTTOM);
picViewer.setBorder(new LineBorder(Color.BLACK, 2));
JMenuBar frameMenuBar = new JMenuBar();
frame.setJMenuBar(frameMenuBar);
JMenu file = new JMenu("File");
frameMenuBar.add(file);
JMenuBar picViewerMenu = new JMenuBar();
picViewerMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
thumbs = new JButton("THUMBNAILS");//an icon in actual program
thumbs.setPreferredSize(new Dimension(150,45));
thumbs.setToolTipText("Thumbnails");
thumbs.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
picViewer.setVisible(false);
thumbPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
thumbPanel.setBorder(BorderFactory.createEmptyBorder(50,100,50,30));
thumbnails = new JButton[30];//example size, chosen so all buttons won't fit on one page
for (int i = 0; i < 30; i++) {
thumbnails[i] = new JButton(Integer.toString(i));
thumbnails[i].setPreferredSize(new Dimension(100, 100));
thumbnails[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("thumbnail clicked - opens full-size view of pic in the JLabel picViewer");
}
});
thumbPanel.add(thumbnails[i]);
thumbPanel.setVisible(true);
}
toolBar = new JToolBar(null, JToolBar.VERTICAL);
toolBar.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 30));
JButton up = new JButton("Up Arrow");
up.setPreferredSize(new Dimension(80,60));
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Up Arrow Stub - NEEDS TO SCROLL UP PAGE, as needed");
}
} );
JButton down = new JButton("Down Arrow");
down.setPreferredSize(new Dimension(80,60));
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Down Arrow Stub - NEEDS TO SCROLL DOWN PAGE, as needed");
}
} );
toolBar.add(Box.createGlue());
toolBar.add(up);
toolBar.add(Box.createVerticalStrut(40));
toolBar.add(down);
toolBar.add(Box.createGlue());
frame.getContentPane().add(thumbPanel, BorderLayout.CENTER);
frame.getContentPane().add(toolBar, BorderLayout.LINE_END);
}
});
picViewerMenu.add(thumbs);
frame.getContentPane().add(picViewerMenu, BorderLayout.SOUTH);
frame.add(picViewer, BorderLayout.CENTER);
frame.setLocation(300, 50);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
PicSlider2 ps = new PicSlider2();
}
});
}
}
我找到了一种方法,通过对JPanel上的项目重新排序来实现您想要的结果。JFrame需要在构造函数之外声明。我添加了一个用于跟踪滚动位置的索引变量和一个用于重新加载重新排序的拇指的reloadThumbs()方法。这是更改后的完整代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class PicSlider2 {
private JButton thumbs;
private JButton[] thumbnails;
private JLabel picViewer;
private JPanel thumbPanel;
private JToolBar toolBar;
private int scrollIndex;
private final JFrame frame;
public PicSlider2() {
scrollIndex = 0;
frame = new JFrame("Picture Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 600));
frame.setResizable(false);
frame.setLayout(new BorderLayout());
picViewer = new JLabel();
picViewer.setText("Image here");
picViewer.setHorizontalAlignment(JLabel.CENTER);
picViewer.setVerticalAlignment(JLabel.BOTTOM);
picViewer.setBorder(new LineBorder(Color.BLACK, 2));
JMenuBar frameMenuBar = new JMenuBar();
frame.setJMenuBar(frameMenuBar);
JMenu file = new JMenu("File");
frameMenuBar.add(file);
JMenuBar picViewerMenu = new JMenuBar();
picViewerMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
thumbs = new JButton("THUMBNAILS");//an icon in actual program
thumbs.setPreferredSize(new Dimension(150,45));
thumbs.setToolTipText("Thumbnails");
thumbs.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
picViewer.setVisible(false);
thumbPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
thumbPanel.setBorder(BorderFactory.createEmptyBorder(50,100,50,30));
thumbnails = new JButton[30];//example size, chosen so all buttons won't fit on one page
for (int i = 0; i < 30; i++) {
thumbnails[i] = new JButton(Integer.toString(i));
thumbnails[i].setPreferredSize(new Dimension(100, 100));
thumbnails[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("thumbnail clicked - opens full-size view of pic in the JLabel picViewer");
}
});
thumbPanel.add(thumbnails[i]);
thumbPanel.setVisible(true);
}
toolBar = new JToolBar(null, JToolBar.VERTICAL);
toolBar.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 30));
JButton up = new JButton("Up Arrow");
up.setPreferredSize(new Dimension(80,60));
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Up Arrow Stub - NEEDS TO SCROLL UP PAGE, as needed");
if(scrollIndex > 3) scrollIndex -= 4;
else scrollIndex = 0;
reloadThumbs();
}
} );
JButton down = new JButton("Down Arrow");
down.setPreferredSize(new Dimension(80,60));
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Down Arrow Stub - NEEDS TO SCROLL DOWN PAGE, as needed");
if(scrollIndex < 27) scrollIndex += 4;
else scrollIndex = 30;
reloadThumbs();
}
} );
toolBar.add(Box.createGlue());
toolBar.add(up);
toolBar.add(Box.createVerticalStrut(40));
toolBar.add(down);
toolBar.add(Box.createGlue());
frame.getContentPane().add(thumbPanel, BorderLayout.CENTER);
frame.getContentPane().add(toolBar, BorderLayout.LINE_END);
}
});
picViewerMenu.add(thumbs);
frame.getContentPane().add(picViewerMenu, BorderLayout.SOUTH);
frame.add(picViewer, BorderLayout.CENTER);
frame.setLocation(300, 50);
frame.pack();
frame.setVisible(true);
}
private void reloadThumbs(){
thumbPanel.removeAll();
for(int i = scrollIndex; i < 30; ++i){
thumbPanel.add(thumbnails[i]);
}
for(int i = 0; i < scrollIndex; ++i){
thumbPanel.add(thumbnails[i]);
}
thumbPanel.revalidate();
frame.revalidate();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
PicSlider2 ps = new PicSlider2();
}
});
}
}
我试图在没有JScrollPane的情况下完成这项工作,因为我需要自定义箭头图标
您可以使用JScrollPane
并使用默认的滚动操作
创建带有自定义图标的按钮
:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
public class ScrollPaneSSCCE extends JPanel
{
public ScrollPaneSSCCE()
{
setLayout( new BorderLayout() );
JTable table = new JTable(50, 5);
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
add(scrollPane);
JScrollBar vertical = scrollPane.getVerticalScrollBar();
JPanel east = new JPanel( new BorderLayout() );
add(east, BorderLayout.EAST);
JButton north = new JButton( new ActionMapAction("UP", vertical, "negativeUnitIncrement") );
east.add(north, BorderLayout.NORTH);
JButton south = new JButton( new ActionMapAction("DOWN", vertical, "positiveUnitIncrement") );
east.add(south, BorderLayout.SOUTH);
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("ScrollPaneSSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ScrollPaneSSCCE());
frame.setSize(200, 300);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
您将需要Action Map Action类,它只是一个简单的包装类,从指定组件的ActionMap中获取默认的Action
。
您需要为面板设置滚动增量。由于您的图像大小是100,您可能想要使用:
vertical.setUnitIncrement( 100 );
问题内容: 如何使JPanel可滚动?我实现了可滚动界面,但在将其添加到包含面板时 什么都行不通 码: 问题答案: 您必须使用。然后致电; 您不必实现可滚动的,JPanel已经可以滚动
问题内容: 我试图将滚动条添加到JTextArea。有人可以告诉我下面的代码我做错了什么吗? 先感谢您。 编辑:我用下面的Adel Boutros的建议修复了代码。 问题答案: 这没有用,因为您没有将ScrollPane附加到JFrame。 另外,您不需要2个JScrollPanes:
我想将添加到中,然后将添加到中。我设法在上显示了,但我想将添加为的第4个选项卡。 显示图表的代码: 要添加到TabbedPane的代码:
我尝试使用和将添加到中,但无法将添加到中。我正在使用NetBeans和后端MySQL。请帮帮我. 编辑:谢谢,但我不知道如何分组。你能帮帮我吗?我有4列。第一列单元格包含项目名称,第二列单元格包含数量,第三列和第四列单元格连接JRadio按钮。然后我想在每一行中分组包含JRadio按钮的第三列和第四列单元格 编辑:如果我试图通过使用这个在jTable的自定义代码中添加radiobutton, ,那
问题内容: 我想用JLabel替换JButton,并且希望我的代码在单击JLabel时执行一些操作。 当我拥有JButton时,我使用动作侦听器来处理按钮的单击: 当我替换为时,我在Eclipse中收到以下错误消息: 对于JLabel类型,未定义方法addActionListener(ChipsListener) 但我确实知道应该可以将单击处理程序附加到JLabel。有人知道该怎么做吗? 问题答案
问题内容: 我想要一个具有大小和定义位置的JPanel。在JPanel内部,我水平插入了一定数量的元素(按钮)。因为我的JPanel具有定义的宽度,所以如果添加很多按钮,则看不到。在这种情况下,我需要为此JPanel使用滚动条。但是,此JPanel必须是JFrame内某个位置的某个特定大小。JPanel的滚动条已在其下方水平放置。有人可以帮我吗?我尝试过没有成功! 问题答案: 使用JScrollP