编辑1:
MigLayout的罐子是:
OP:
我有一个JPanel作为Java Swing应用程序的“主屏幕”。它不可调整大小。在这个主屏幕中,我有按钮将用户引导到应用程序的不同功能/部分,每个功能都在不同的JPanel中。我使用CardLayout组织这些面板,将它们放在JPanel panelCentro
中,然后将此面板添加到主屏幕的contentPane中。
在主框架中:
panelCentro.setLayout(new CardLayout());
//getContentPane().setLayout(null);
getContentPane().add(panelCentro);
panelCentro
有背景图片。我覆盖了此面板的paintComponent
方法,以绘制图像。
只有在null
中使用panelCentro
作为布局管理器时,才能正常工作。问题在于功能面板应该接受调整大小。
当调整大小时,主屏幕容器也会调整大小,如果我使用一些布局管理器来管理panelCentro
,图像也会调整大小,因为(我猜)布局管理器会调用{因此,{1}}调用repaint()
来绘制背景。
但是,请记住主屏幕不可调整大小,当我单击“返回”按钮返回主屏幕时,它将返回原始大小,并且在缩放完成之前将显示背景图像,因此,扭曲。
在这里你有一个SSCCE,以某种方式显示图像是如何扭曲的。图像如下所示。下载并将其重命名为“back.png”,并将其移至项目的paintComponent()
目录。最大化窗口,继续单击“切换”按钮,每次显示面板时图像都会展开(您注意到按钮持续向下移动),并且在大约10次单击时,它会停止调整大小。
img
我想要实现的目标是:
我现在所做的是使用脏布局和脏手工作(听import java.awt.CardLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import net.miginfocom.swing.MigLayout;
public class AutoResizingProblem extends JFrame {
private static final Font ARIAL_PLAIN_15 = new Font("Arial", Font.PLAIN, 15);
private static final Font ARIAL_BOLD_14 = new Font("Arial", Font.BOLD, 14); //FONT++
private static final Font ARIAL_PLAIN_14 = new Font("Arial", Font.PLAIN, 14); //font++
private static final Font CONSOLAS_PLAIN_14 = new Font("Consolas", Font.PLAIN, 14); //FONT++
private static final Font ARIAL_PLAIN_13 = new Font("Arial", Font.PLAIN, 13);//font++
private JPanel panel2;
public AutoResizingProblem() {
begin();
}
private void begin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new MigLayout("insets 5, fillx", "[]", "[]5[]"));
final JPanel content = new JPanel();
getContentPane().add(content, "cell 0 0, grow");
final JPanel panel = new JPanel();
panel.setLayout(new MigLayout("insets 5 5 5 5", "[fill, grow]5[300!]", "[]10[]5[fill, grow]5[]5[]"));
panel.setOpaque(false);
panel.setVisible(true);
final JLabel lblreconstruccinDelRegistro = new JLabel("<html>Reconstrucci\u00F3n del Registro de Env\u00EDos y Respuestas</html>");
lblreconstruccinDelRegistro.setToolTipText("<html>Reconstrucci\u00F3n del Registro de Env\u00EDos y Respuestas</html>");
lblreconstruccinDelRegistro.setOpaque(true);
lblreconstruccinDelRegistro.setHorizontalTextPosition(SwingConstants.CENTER);
lblreconstruccinDelRegistro.setHorizontalAlignment(SwingConstants.CENTER);
lblreconstruccinDelRegistro.setForeground(Color.BLACK);
lblreconstruccinDelRegistro.setFont(ARIAL_PLAIN_15);
lblreconstruccinDelRegistro.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
lblreconstruccinDelRegistro.setBorder(new LineBorder(Color.GRAY));
lblreconstruccinDelRegistro.setBackground(UIManager.getColor("Menu.background"));
panel.add(lblreconstruccinDelRegistro, "cell 0 0, span 2, h 25!, grow");
JLabel lblSeguimiento = new JLabel("Seguimiento actual:");
lblSeguimiento.setToolTipText("Seguimiento actual");
lblSeguimiento.setFont(ARIAL_BOLD_14);
panel.add(lblSeguimiento, "cell 0 1, split 3, h 25!");
JRadioButton rbMantener = new JRadioButton();
rbMantener.setText("Mantener");
rbMantener.setFont(ARIAL_PLAIN_14);
panel.add(rbMantener, ""); //add after the label of Seuigimiento, same cell.
JRadioButton rbEliminar = new JRadioButton();
rbEliminar.setText("Eliminar");
rbEliminar.setFont(ARIAL_PLAIN_14);
panel.add(rbEliminar, "grow");
ButtonGroup group = new ButtonGroup();
group.add(rbMantener);
group.add(rbEliminar);
JButton btnIniciarRBD = new JButton("Iniciar Reconstrucci\u00F3n");
btnIniciarRBD.setFont(ARIAL_PLAIN_14);
panel.add(btnIniciarRBD, "cell 1 1, gapleft push, h 30!, gapright 5");
JPanel panelListasPorProcesar = new JPanel();
panelListasPorProcesar.setLayout(new MigLayout("insets 0 0 0 0, fillx, debug","[50%]5[50%]", "[25!]5[45%]5[45%]"));
panelListasPorProcesar.setOpaque(false);
panelListasPorProcesar.setBorder(null);
// panelListasPorProcesar.setBounds(10, 100, 440, 270);
panel.add(panelListasPorProcesar, "cell 0 2, grow");
// Label "Número de ficheros por procesar"
JLabel lblNmeroDeFicherosPorProcesar = new JLabel();
lblNmeroDeFicherosPorProcesar.setFont(ARIAL_BOLD_14);
panelListasPorProcesar.add(lblNmeroDeFicherosPorProcesar, "cell 0 0, grow, span 2");
JLabel lblBuznDeEntrada = new JLabel();
lblBuznDeEntrada.setFont(ARIAL_PLAIN_14);
;
panelListasPorProcesar.add(lblBuznDeEntrada, "cell 0 1, split 2, flowy, h 25!, grow");
final JList listaRB01 = new JList();
listaRB01.setOpaque(true);
listaRB01.setBorder(null);
listaRB01.setFont(CONSOLAS_PLAIN_14);
JScrollPane spRB01 = new JScrollPane();
spRB01.getViewport().setOpaque(false);
spRB01.setViewportView(listaRB01);
panelListasPorProcesar.add(spRB01, "cell 0 1, grow");
JLabel lblFicherosRnt = new JLabel();
lblFicherosRnt.setFont(ARIAL_PLAIN_14);
panelListasPorProcesar.add(lblFicherosRnt, "cell 1 1, flowy, split 2, h 25!, grow");
JList listaRB02 = new JList();
listaRB02.setOpaque(true);
listaRB02.setBorder(null);
listaRB02.setFont(CONSOLAS_PLAIN_14);
JScrollPane spRB02 = new JScrollPane();
spRB02.getViewport().setOpaque(false);
spRB02.setViewportView(listaRB02);
panelListasPorProcesar.add(spRB02, "cell 1 1, grow");
JLabel lblFicherosDcl = new JLabel();
lblFicherosDcl.setFont(ARIAL_PLAIN_14);
panelListasPorProcesar.add(lblFicherosDcl, "cell 0 2, split 2, flowy, h 25!, grow");
// Lista "Ficheros DCL"
JList listaRB03 = new JList();
listaRB03.setOpaque(true);
listaRB03.setFont(CONSOLAS_PLAIN_14);
JScrollPane spRB03 = new JScrollPane();
spRB03.getViewport().setOpaque(false);
spRB03.setViewportView(listaRB03);
// panelListasPorProcesar.add(spRB03);
panelListasPorProcesar.add(spRB03, "cell 0 2, grow");
JLabel lblFicherosRlc = new JLabel();
lblFicherosRlc.setFont(ARIAL_PLAIN_14);
panelListasPorProcesar.add(lblFicherosRlc, "cell 1 2, split 2, flowy, h 25!, grow");
// Lista "Ficheros RLC"
JList listaRB04 = new JList();
listaRB04.setOpaque(true);
listaRB04.setBorder(null);
listaRB04.setFont(CONSOLAS_PLAIN_14);
JScrollPane spRB04 = new JScrollPane();
spRB04.getViewport().setOpaque(false);
spRB04.setViewportView(listaRB04);
panelListasPorProcesar.add(spRB04, "cell 1 2, grow");
JLabel lblNmeroDeFicherosProcesados = new JLabel();
lblNmeroDeFicherosProcesados.setFont(ARIAL_BOLD_14);
panel.add(lblNmeroDeFicherosProcesados, "cell 1 2, grow, split 2, flowy, align left, h 25!");
JList listaRBProcesados = new JList();
listaRBProcesados.setOpaque(true);
listaRBProcesados.setBorder(null);
listaRBProcesados.setFont(CONSOLAS_PLAIN_14);
JScrollPane spRBProcesados = new JScrollPane();
spRBProcesados.setBorder(new LineBorder(Color.DARK_GRAY));
spRBProcesados.getViewport().setOpaque(false);
spRBProcesados.setViewportView(listaRBProcesados);
panel.add(spRBProcesados, "cell 1 2, grow");
JLabel lblAvance = new JLabel("A long bar");
lblAvance.setFont(ARIAL_PLAIN_14);
lblAvance.setForeground(Color.BLACK);
panel.add(lblAvance, "cell 0 3, span 2, grow");
JButton button = new JButton("Toggle");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
CardLayout cl = null;
cl = (CardLayout)content.getLayout();
if (panel.isVisible()) {
System.out.println("show panel2");
cl.show(content, "panel2");
} else if (panel2.isVisible()) {
System.out.println("show panel1");
cl.show(content, "panel1");
}
}
});
getContentPane().add(button, "cell 0 1, grow");
content.setLayout(new CardLayout());
content.add(panel, "panel1");
panel2 = new JPanel() {
@Override
public void paintComponent(Graphics g){
Dimension tamanio = getSize();
ImageIcon imagenDeFondo = new ImageIcon("img/back.png");
g.drawImage(imagenDeFondo.getImage(), 0, 0, tamanio.width, tamanio.height, null);
setOpaque(false);
// super.paintComponent(g);
}
// @Override
// public Dimension getPreferredSize() {
// return new Dimension(600,400);
// }
};
content.add(panel2, "panel2");
// setContentPane(panel);
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
AutoResizingProblem frame = new AutoResizingProblem();
}
});
}
}
。但这违背了许多学说。有没有办法用一些布局管理器做这个?如果不可能的话,我想说绝对定位有时是有用的。
答案 0 :(得分:1)
代码不是SSCCE - 缺少MigLayout - 所以我无法测试它!
如果您不希望缩放图像,请使用不带大小的drawImage方法:
public void paintComponent(Graphics g) {
...
g.drawImage(imagenDeFondo.getImage(), 0, 0, null);
...
这个答案主要基于问题的标题