小弟的代码想实现如下功能:
1. 从桌面拖拽文件到JFrame里, 并显示一个button
2. 在JFrame里的button每排显示4个, 多余4个换行显示.
3. 当button显示到JFrame底部的时候出现JScrollPane, 并且可以继续添加.
我的代码如下
- Java code
import java.awt.*;import java.awt.dnd.*;import java.awt.event.*;import java.util.Locale;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.*;public class DesktopTools extends JFrame implements DropTargetListener { private JScrollPane scrollPane; private JPanel panel; DropTarget dropTarget; private int i, j; public DesktopTools() { setTitle("Tools"); setSize(370, 200); setLocation(100, 100); this.setAlwaysOnTop(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this, true, null); panel = new JPanel(); scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); this.getContentPane().add(scrollPane); setVisible(true); setResizable(false); this.repaint(); } public static void main(String args[]) { DesktopTools dt = new DesktopTools(); } @Override public void dragEnter(DropTargetDragEvent dtde) {// throw new UnsupportedOperationException("Not supported yet."); } @Override public void dragOver(DropTargetDragEvent dtde) {// throw new UnsupportedOperationException("Not supported yet."); } @Override public void dropActionChanged(DropTargetDragEvent dtde) {// throw new UnsupportedOperationException("Not supported yet."); } @Override public void dragExit(DropTargetEvent dte) {// throw new UnsupportedOperationException("Not supported yet."); } @Override public void drop(DropTargetDropEvent dtde) {// throw new UnsupportedOperationException("Not supported yet."); int bWidth = 80; int bHeight = 80; if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) { System.out.println("test"); JButton jb = new JButton("test" + i); panel.add(jb); jb.setSize(bWidth, bHeight); jb.setLocation(5 + (i % 4) * (bWidth + 2), 20 + (j * (bHeight + 2))); if (((i + 1) % 4) == 0) { j++; } i++; [color=#FFFF00]//panel.updateUI();[/color] [color=#FF0000]panel.repaint();[/color] this.repaint(); } }}
这段代码可以大概实现上述要求. 但是如果用panel.repaint方法, 的确可以换行,但是到了JFrame的底部, JScrollPane 并未出现.
如果用panel.updateUI, 遇到的问题就是, 全都显示在一排. 现在的确不知该如何处理了. 请各位帮帮忙.
谢谢了
我用的编辑工具是 NetBeans
------解决方案--------------------
JAVA swing很久没用过了、建议楼主查下JScrollPane api文档 。
祝早日解决~
------解决方案--------------------
- Java code
@Override public void drop(DropTargetDropEvent dtde) { // throw new UnsupportedOperationException("Not supported yet."); int bWidth = 80; int bHeight = 80; if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) { System.out.println("test"); JButton jb = new JButton("test" + i); panel.add(jb); Dimension d1 = new Dimension(bWidth, bHeight); jb.setPreferredSize(d1); jb .setLocation(5 + (i % 4) * (bWidth + 2), 20 + (j * (bHeight + 2))); if (((i + 1) % 4) == 0) { Dimension d = new Dimension(this.getWidth(), this.getHeight() + j * bHeight); panel.setPreferredSize(d); j++; } i++; panel.updateUI(); panel.repaint(); this.repaint(); } }