[英]How to stop components adapting to the size of a JPanel that is inside a JScrollPane
我获取了图像中可见的信息,并创建了一个示例 Swing GUI 来显示相同的信息。
我不知道我的代码与您的代码有何不同。 我在下载的JPanel
中放置了一个JLabel
和两个JTextAreas
。 我创建了一个主JPanel
来保存下载JPanels
。
这是我使用的完整的可运行代码。 我将附加类设为内部类,这样我就可以将代码作为一个块发布。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class MusicDownloadGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new MusicDownloadGUI());
}
private final MusicDownloadModel model;
public MusicDownloadGUI() {
this.model = new MusicDownloadModel();
}
@Override
public void run() {
JFrame frame = new JFrame("Music Download");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createMainPanel() {
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
for (Track track : model.getDownloads()) {
panel.add(createDownloadPanel(track));
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return scrollPane;
}
private JPanel createDownloadPanel(Track track) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
panel.setBackground(Color.WHITE);
JLabel label = new JLabel(new ImageIcon(track.getAlbumCover()));
panel.add(label);
JTextArea titleArea = new JTextArea(3, 30);
titleArea.setEditable(false);
titleArea.setText(createTitleArea(track));
panel.add(titleArea);
JTextArea artistArea = new JTextArea(3, 25);
artistArea.setEditable(false);
artistArea.setText(createArtistArea(track));
panel.add(artistArea);
return panel;
}
private String createTitleArea(Track track) {
StringBuilder builder = new StringBuilder();
builder.append(track.getTitle());
builder.append(System.lineSeparator());
builder.append("Download -- ");
builder.append(track.getPercentDownloaded());
builder.append("%");
// System.out.println(builder.toString());
return builder.toString();
}
private String createArtistArea(Track track) {
StringBuilder builder = new StringBuilder();
builder.append("Artist: ");
builder.append(track.getArtist());
builder.append(System.lineSeparator());
builder.append("Duration: ");
int minutes = track.getDuration() / 60;
int seconds = track.getDuration() % 60;
builder.append(minutes);
builder.append(" minutes, ");
builder.append(seconds);
builder.append(" seconds");
builder.append(System.lineSeparator());
builder.append("Quality: ");
builder.append(track.getQuality());
return builder.toString();
}
public class MusicDownloadModel {
private final List<Track> downloads;
public MusicDownloadModel() {
this.downloads = new ArrayList<>();
Track track = new Track("Bandaga", "Otra Copa", "0.6 kbytes", 216);
track.setPercentDownloaded(42);
addDownload(track);
addDownload(new Track("Bandaga", "Ahora Dice Que Me Ama", "0.6 kbytes", 198));
addDownload(new Track("Camin", "Tu Nombre (feat. JC Reyes, El Daddy) - Remix",
"0.6 kbytes", 246));
addDownload(new Track("Bandaga", "Como Tu", "0.6 kbytes", 205));
addDownload(new Track("Pablo Alboran", "Liueve Sobre Mo Jado", "0.6 kbytes", 228));
}
public void addDownload(Track track) {
this.downloads.add(track);
}
public List<Track> getDownloads() {
return downloads;
}
}
public class Track {
private int percentDownloaded;
private final int duration;
private final String artist, quality, title;
private final BufferedImage albumCover;
public Track(String artist, String title, String quality, int duration) {
this.artist = artist;
this.title = title;
this.quality = quality;
this.duration = duration;
this.percentDownloaded = 0;
this.albumCover = new BufferedImage(40, 20, BufferedImage.TYPE_INT_RGB);
Graphics g = albumCover.getGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, albumCover.getWidth(), albumCover.getHeight());
g.dispose();
}
public int getPercentDownloaded() {
return percentDownloaded;
}
public void setPercentDownloaded(int percentDownloaded) {
this.percentDownloaded = percentDownloaded;
}
public int getDuration() {
return duration;
}
public String getArtist() {
return artist;
}
public String getQuality() {
return quality;
}
public String getTitle() {
return title;
}
public BufferedImage getAlbumCover() {
return albumCover;
}
}
}
BoxLayout 尊重添加到其中的组件的最小和最大尺寸。
默认情况下,JPanel 没有最大大小,因此它会增长以填充可用空间。
因此,一种解决方案是覆盖面板的getMaximumSize()
方法,使其等于首选大小。
a) 如下运行代码以查看默认行为。 单击“添加行”按钮以查看添加行时布局如何更改。
b) 然后在更改getMaximumSize()
方法以使用首选大小后重复。
import java.awt.*;
import javax.swing.*;
public class BoxLayoutRow extends JPanel
{
private int rowIndex;
private JPanel rowPanel;
public BoxLayoutRow()
{
setLayout( new BorderLayout() );
rowPanel = new JPanel();
rowPanel.setLayout( new BoxLayout(rowPanel, BoxLayout.Y_AXIS) );
add(new JScrollPane(rowPanel), BorderLayout.CENTER);
JButton addRow = new JButton("Add Row");
add(addRow, BorderLayout.PAGE_END);
addRow.addActionListener((e) -> this.addRow());
}
private void addRow()
{
rowIndex++;
JPanel row = new JPanel()
{
@Override
public Dimension getMaximumSize()
{
return super.getMaximumSize();
//return getPreferredSize();
}
};
row.setAlignmentX(0.0f);
row.add( new JLabel("Row: " + rowIndex) );
row.setBorder(BorderFactory.createLineBorder(Color.RED));
rowPanel.add( row );
revalidate();
repaint();
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("BoxLayoutRow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new BoxLayoutRow() );
frame.setSize(400, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(() -> createAndShowUI());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.