Builder.cz - Informacni server o programovani

Odběr fotomagazínu

Fotografický magazín "iZIN IDIF" každý týden ve Vašem e-mailu.
Co nového ve světě fotografie!

 

Zadejte Vaši e-mailovou adresu:

Kamarád fotí rád?

Přihlas ho k odběru fotomagazínu!

 

Zadejte e-mailovou adresu kamaráda:



Repaint nefunguje

Seznam témat     Nová odpověď

Přihlásit se     Registrace     Zapomenuté heslo

Re: Repaint nefunguje

Autor: Miromancer

13:59:15 11.11.2011

Díky! Bylo to tím Labelem, což byl překlep.
Teď už to funguje, jak jsem chtěl.

Citovat příspěvek

 

Re: Repaint nefunguje

Autor: mKorbel

13:28:31 11.11.2011

1) playerPanel.add(new Label(name));

nie je dobre mixovat AWT a Swing takze

playerPanel.add(new JLabel(name));

2) parent.removeAll();

add(cokolvek);
revalidate();
repaint();

3) toto cele je o http://download.oracle.com/javase/tutorial/uiswing/layout/index.html

- ked ta nebude Size() a setBounds()
- ked tam bude Cardlayout http://download.oracle.com/javase/tutorial/uiswing/layout/card.html
- alebo ked tam bude BoderLayout, tak na konkretnu BoderLayout.CENTER, NOTHR... je mozne dat iba jeden komponent (by default vlastnost Cardlayout), potom tam nie je potrebne ziadne remove(), removaAll()

addedPlayersPanel.add(playerPanel, BoderLayout.CENTER);
addedPlayersPanel.revalidate();
addedPlayersPanel.repaint();

4) Absolutelayout je o manualnom retazeni a opisani vsetkych Eventov, ktore su by default implementovane pre vsetky ostatne LayoutManagers

Citovat příspěvek

 

Re: Repaint nefunguje

Autor: Miromancer

12:26:34 11.11.2011

to prave volam:

addedPlayersPanel.validate();
addedPlayersPanel.repaint();

Citovat příspěvek

 

Re: Repaint nefunguje

Autor: marekStu

9:50:30 11.11.2011

Zkus zavolat metodu validate() po smazani.

Citovat příspěvek

 

Repaint nefunguje

Autor: Miromancer

9:50:28 11.11.2011

Pokud pridam po vymazani dalsi panel, ty labely z nepremalovaneho panelu porad nejsou videt.
Diky moc za pripadne rady, tady dodavam kod (jedna se o metodu removePlayer:

public class GameStart extends JFrame {

private int playersCount = 0;
private JLabel playerLabel;
private JTextField playerName;
private JComboBox playerType;
private ColorComboBox playerColor;
private JButton addPlayerButton;
private ArrayList playersPanels;
private ArrayList players;

private final String[] colorStrings = { "Blue", "Yellow", "Green",
"Red", "Purple", "Teal" };
private final Integer[] colorValues = { Game.BLUE, Game.YELLOW, Game.GREEN,
Game.RED, Game.PURPLE, Game.TEAL };
private JPanel addedPlayersPanel;

public GameStart(String s)
{
super(s);
createAndShowGUI();
}



private void createAndShowGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(640, 480));
setResizable(false);
setLayout(null);
Dimension size;

int x = 20;
int xOrig = x;
int y = 50;
final int insetX = 10;
final int insetY = 10;
final int MAX_PLAYERS = 5;

playerLabel = new JLabel("Player: ");
size = playerLabel.getPreferredSize();
add(playerLabel);
playerLabel.setBounds(x, 20, size.width, 20);
x += size.width + insetX;

playerName = new JTextField("Enter Name");
size = playerName.getPreferredSize();
playerName.setBounds(x, 20, 150, 20);
x += 150 + insetX;

add(playerName);
playerName.addFocusListener(new FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
playerNameFocusGained(evt);
}
});

playerType = new JComboBox(new String[] { "Human", "CPU" });
size = playerType.getPreferredSize();
playerType.setBounds(x, 20, size.width, 20);
add(playerType);
x += size.width + insetX;
String[] colorStrings = { "Blue", "Yellow", "Green",
"Red", "Purple", "Teal" };

playerColor = new ColorComboBox(colorValues, colorStrings);
add(playerColor);
size = playerColor.getPreferredSize();
playerColor.setBounds(x, 20, size.width, 20);
x += size.width + insetX;

addPlayerButton = new JButton("Add Player");
size = addPlayerButton.getPreferredSize();
add(addPlayerButton);
addPlayerButton.setBounds(x, 20, size.width, 20);
x+= size.width;

addedPlayersPanel = new JPanel(new GridLayout(MAX_PLAYERS, 1));
addedPlayersPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
addedPlayersPanel.setBounds(xOrig, y, x - xOrig, (y + insetY) *MAX_PLAYERS);
add(addedPlayersPanel);

addPlayerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addPlayerToPanel();
}
});
playersPanels = new ArrayList();
players = new ArrayList();

pack();
}
private void playerNameFocusGained(java.awt.event.FocusEvent evt) {
selectText((JTextField)evt.getSource());
}
private void selectText(JTextField f)
{
f.selectAll();
}

private void addPlayerToPanel()
{
if (playersCount >= 5) return;
String name = playerName.getText();
int type = (Integer)playerType.getSelectedIndex();
int index = (Integer)playerColor.getSelectedItem();
if (index == -1) return;

String color = colorStrings[index];

Player player = new Player(name, type, colorValues[index]);
players.add(player);
playersCount++;

JButton remButton = new JButton("Remove Player");
remButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
removePlayer(e);
}
});

JPanel playerPanel = new JPanel(new GridLayout(1, 4));
playerPanel.add(new Label(name));
playerPanel.add(new Label(color));
playerPanel.add(new Label((String)playerType.getSelectedItem()));
playerPanel.add(remButton);

playersPanels.add(playerPanel);
addedPlayersPanel.add(playerPanel);
addedPlayersPanel.revalidate();

}

private void removePlayer(ActionEvent evt)
{
Component comp = (Component) evt.getSource();
JPanel parent = (JPanel) comp.getParent();
parent.removeAll();
playersCount--;
int index = playersPanels.indexOf(parent);
System.out.println(index);
players.remove(index);
playersPanels.remove(parent);
// setModelForPanel(addedPlayersPanel, playersPanels);

/* for (Component c: addedPlayersPanel.getComponents())
{
JPanel panel = (JPanel)c;
panel.revalidate();
panel.repaint();
}
playersPanels.remove(parent);*/
//addedPlayersPanel.remove(parent);
addedPlayersPanel.validate();
addedPlayersPanel.repaint();

/* for (Component c: addedPlayersPanel.getComponents())
{
JPanel panel = (JPanel)c;
panel.validate();
panel.repaint();
}*/


}

Citovat příspěvek

 

Repaint nefunguje

Autor: Miromancer

9:40:44 11.11.2011

Zdravim,

mam miniaplikaci, kde je panel, do ktereho vkladam dalsi panely (vkladane panely obsahuji JLabely a jeden JButton na vymazani daneho panelu).
Vkladani funguje dobre, ale mazani uz nikoliv.
Kdyz se pokusim vymazat panel, který není v posledni radce, tak se zdanlive "vymaze" i ten nad nim (respektive jen labely, tlacitko zustane). Spravi se to az teprve kdyz vynutim repaint minimalizaci nebo dragem mimo obrazovku.

Citovat příspěvek

 

 

 

Přihlášení k mému účtu

Uživatelské jméno:

Heslo: